Ruby 语言学习demo 笔记

在iOS Podfile开发过程中, 以及Flutter自身工程中,都会遇到一些Ruby脚本解决工程架构问题;每次隔一段时间总会忘记,故梳理一个文档记录遇到的脚本功能,并写下测试demo,便于下次回归;

 

Mac 下脚本的运行方式: 下载Sumlime Text, 通过Command+B的方式编译;

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

a = [1,'hi', 3.14, 1, 2, [4, 5]]

p a[2]           # 3.14
p a.[](2)
p a.reverse      # [[4, 5], 2, 1, 3.14, 'hi', 1]
p a.flatten.uniq # [1, 'hi', 3.14, 2, 4, 5]

puts("-------------------------------------------")
hash = { :water => 'wet', :fire => 'hot' }
puts hash[:fire] # Prints:  hot

hash.each_pair do |key, value| # Or:  hash.each do |key, value|
	puts "#{key} is #{value}"
end

# Prints:  water is wet
#          fire is hot

hash.delete :water # Deletes :water => 'wet'
hash.delete_if {|k,value| value=='hot'} # Deletes :fire => 'hot'


puts("-------------------------------------------")

def remember(&a_block)
  @block = a_block
end

# Invoke the above method, giving it a block which takes a name.
remember {
	|name| puts "Hello, #{name}!"
}

# When the time is right (for the object) -- call the closure!
@block.call('cat');
# => "Hello, Jon!"


puts("-------------------------------------------")

def create_set_and_get(initial_value=0) # Note the default value of 0
  closure_value = initial_value
  return Proc.new {|x| closure_value = x}, Proc.new { puts closure_value }
end

setter, getter = create_set_and_get  # ie. returns two values
setter.call(21)
getter.call # => 21

puts("-------------------------------------------")

def use_hello
  yield "hello"
end

# Invoke the above method, passing it a block.
use_hello {
|string| puts string
} # => 'hello'

def my_method
	yield("makuang",32)
end

my_method do |name, age|
	puts "#{name} is #{age} years old"
end

puts("-------------------------------------------")

class Person
  attr_reader :name, :age
  def initialize(name, age)
    @name, @age = name, age
  end
  def <=>(person) # Comparison operator for sorting
    @age <=> person.age
  end
  def to_s
    "#@name (#@age)"
  end
end

group = [
  Person.new("Bob", 33),
  Person.new("Chris", 16),
  Person.new("Ash", 23)
]

puts group.sort.reverse

puts("-------------------------------------------")

afile = File.new("test1.txt", "w+")
if afile
	afile.syswrite("ABCDEF")
	# afile.rewind
	# afile.lineno
	afile.each_byte{|ch| puts ch; }
	afile.readlines[0]
else
	puts "unable to open file"

afile.close; 
end

puts("-------------------------------------------")


puts Dir.pwd

# Dir.chdir("/usr/bin")

# puts Dir.pwd


puts("-------------------------------------------")

def func1
   i=0
   while i<=2
      puts "func1 at: #{Time.now}"
      sleep(2)
      i=i+1
   end
end
 
def func2
   j=0
   while j<=2
      puts "func2 at: #{Time.now}"
      sleep(1)
      j=j+1
   end
end
 
puts "Started At #{Time.now}"
t1=Thread.new{func1()}
t2=Thread.new{func2()}
# t1.join
# t2.join
puts "End at #{Time.now}"


puts("-------------------------------------------")
require 'rubygems'
require 'json'
require 'pp'
 
json = File.read('input.json')
obj = JSON.parse(json)
 
pp obj


puts("-------------------------------------------")

require 'pry'; 
# binding.pry

def parse_KV_file(file, separator='=')
  file_abs_path = File.expand_path(file)
  puts 'file_abs_path: ' + "#{file_abs_path}"
  if !File.exists? file_abs_path
    return [];
  end
  pods_ary = []
  skip_line_start_symbols = ["#", "/"]
  File.foreach(file_abs_path) { |line|
  		# puts 'line: ' + "#{line}"
      next if skip_line_start_symbols.any? { |symbol| line =~ /^\s*#{symbol}/ 
  # puts 'symbol: ' + "#{symbol}"
}
      plugin = line.split(pattern=separator)
      if plugin.length == 2
        podname = plugin[0].strip()
        path = plugin[1].strip()
        podpath = File.expand_path("#{path}", file_abs_path)
        pods_ary.push({:name => podname, :path => podpath});
      else
        puts "Invalid plugin specification: #{line}"
      end
  }
  return pods_ary
end

test2 = parse_KV_file('Generated.xcconfig')
puts test2

# test symlink function, 将test/plugins 指向“根路径”
symlink = File.join('test', 'plugins')
File.symlink(File.expand_path('./'), symlink)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值