Maison  >  Questions et réponses  >  le corps du texte

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中文网2709 Il y a quelques jours547

répondre à tous(1)je répondrai

  • 天蓬老师

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

    Personnellement, je comprends que lorsque le pouvoir est délégué à l'appelant, le blocage peut gagner une plus grande liberté

    répondre
    0
  • Annulerrépondre