def block_args_test
yield()
yield(1)
yield(1, 2, 3)
end
puts "通过|a|接收块变量"
block_args_test do |a|
p [a]
end
puts "通过|a, b, c|接收块变量"
block_args_test do |a, b, c|
p [a, b, c]
end
puts "通过|*a|接收块变量"
block_args_test do |*a|
p [a]
end
# ruby block_args_test.rb
通过|a|接收块变量
[nil]
[1]
[1]
通过|a, b, c|接收块变量
[nil, nil, nil]
[1, nil, nil]
[1, 2, 3]
通过|*a|接收块变量
[[]]
[[1]]
[[1, 2, 3]]
天蓬老师2017-04-24 16:01:24
My personal understanding is that when power is delegated to the caller, the block can gain greater freedom