2009-11-14 3 views

답변

28

루비 1.9이 있습니다 가난한 사람의 기본 차단 인수 :

def call_it &block 
    block.call 
end 

call_it do |*args| 
    x = args[0] || "foo" 
    p "Called the block with value #{x}" 
end 
+2

... 저는 1.8.7에 있는데, 왜 저에게 효과가 없는지 설명합니다. : - \ –

17

:

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 
    ^
관련 문제