传递块作为参数

问题描述 投票:0回答:1

将一个块在其他参数中传递给Ruby中的方法的正确方法是什么?我有这个代码:

def Foo(arg1, &block1)
  puts arg1
  block1.call
end

这不起作用:

Foo('hello', { puts 'world' })
ruby
1个回答
2
投票

像这样

Foo('hello') { puts 'world' }
#hello
#world

另请注意,方法名称应全部小写(和snake_cased)。

© www.soinside.com 2019 - 2024. All rights reserved.