Li Yafei
A senior Ruby on Rails developer
# 关键字参数
# required: ruby2.0.x
#
def method(a, b: 1, c: 2)
puts a
puts b
puts c
end
method(0, b: 1, c: 3) # 输出: a=0, b=1, c=3
method(0, b: 1) # 输出: a=0, b=1, c=2
require 'pp'
a = %i(a b c)
pp a #输出: [:a, :b, :c]
class Base def save puts "base:save" end end module Helper def save puts "helper:save:start" super puts "helper:save:end" end end class For < Base prepend Helper # use prepend instead of include
def save puts "for:save" end end For.new.save # 输出: helper:save:start # for:save # helper:save:end
puts (0..Float::INFINITY).lazy.map {|i| ((-1) ** i) / (2*i + 1).to_f }.take(65536).reduce(:+) * 4
# 输出: 3.141577394800698
puts __dir__ # 等于过去的 File.dirname(__FILE__)
puts nil.to_h
puts ENV.to_h # ENV 是 Object,所以不能直接用
os = OpenStruct.new
os.a = 1
puts os.to_h # { :a=>1 }
# 很多内建的 to_h 调用
IO#select
By Li Yafei