17
This question处理传递给Ruby块的可选参数。我想知道是否也可以用默认值定义参数,以及它的语法是什么。是否可以在Ruby中使用默认参数定义一个块?
乍一看,似乎答案是 “不”:
def call_it &block
block.call
end
call_it do |x = "foo"|
p "Called the block with value #{x}"
end
...结果:
my_test.rb:5: syntax error, unexpected '=', expecting '|'
call_it do |x = "foo"|
^
my_test.rb:6: syntax error, unexpected tSTRING_BEG, expecting kDO or '{' or '('
p "Called the block with value #{x}"
^
my_test.rb:7: syntax error, unexpected kEND, expecting $end
end
^
...我在1.8.7,这解释了为什么它不适合我。 : - \ – 2009-11-14 21:29:31