search

Home  >  Q&A  >  body text

ruby - 以下block例子通常会用在什么场景?

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

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中文网2760 days ago590

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-24 16:01:24

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

    reply
    0
  • Cancelreply