ruby - 以下block例子通常会用在什么场景?
PHP中文网
PHP中文网 2017-04-24 15:59:24
0
1
619

这样例子通常用在什么场景?

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]]
PHP中文网
PHP中文网

认证0级讲师

reply all(1)
Peter_Zhu

My personal understanding is that when power is delegated to the caller, the block can gain greater freedom

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template