Why does this not work for Ruby 1.9.2?
The following code doesn't work with Ruby 1.9.2:
def side_b开发者_高级运维ox(title, &block)
render :layout => 'layouts/side_box', :locals => {:title => title} do
&block
end
end
I am getting an error:
syntax error, unexpected tAMPER &block
What has changed (since 1.8.7) for this not to work?
def side_box(title, &block)
render :layout => 'layouts/side_box', :locals => {:title => title} do
yield
end
end
or
def side_box(title, &block)
render :layout => 'layouts/side_box', :locals => {:title => title} do
block.call
end
end
It works in Ruby 1.9 and 1.8.
I don't have 1.8.7 installed at the moment, but I'm pretty sure that this is illegal in 1.8.7, as well. The unary prefix &
operator is only legal in parameter lists and argument lists.
Building on Simone's answer, in case your block takes arguments (arg1, arg2), the correct syntax (in both 1.8 and 1.9) would be
def side_box(title, &block)
render :layout => 'layouts/side_box', :locals => {:title => title} do
yield(arg1, arg2)
end
end
or
def side_box(title, &block)
render :layout => 'layouts/side_box', :locals => {:title => title} do
block.call(arg1, arg2)
end
end
i didn't know, that this works in 1.8.7, the correct syntax should be
block.call
or
block[]
or
render :layout => 'layouts/side_box', :locals => {:title => title}, &block
精彩评论