Justin Collins
1/17/2008 9:33:00 PM
Joshua Beall wrote:
> Andrew Stewart wrote:
>
>> On 17 Jan 2008, at 16:21, Joshua Beall wrote:
>> So if you look at your block again:
>>
>> do |format|
>> format.html
>> format.xml
>> end
>>
>> ...and you imagine executing it as a method, the block variable,
>> format, should look like a method argument. Accordingly it is set to
>> the responder object passed in by the line:
>>
>> block.call(responder)
>>
>
> Ok, I think I'm with you. Correspondingly that would mean that I can
> replace this
>
> respond_to do |format|
> format.html # index.html.erb
> format.xml { render :xml => @posts }
> end
>
> With this:
>
> responder = Responder.new(self)
> responder.html
> responder.xml { render :xml => @posts}
> responder.respond
>
> And it should work. And indeed it does. However, there are still
> somethings I don't understand. Why can't I do this?
>
> responder = Responder.new(self)
> responder.html
> responder.xml( { render :xml => @posts} )
> responder.respond
>
> If I do that, I get the error (point to the line where I added
> parentheses):
>
> posts_controller.rb:9: syntax error, unexpected tSYMBEG, expecting kDO
> or '{' or '('
>
> It's complaining about the symbol :xml.
>
In this case { render :xml => @posts} is being interpreted as a Hash, not a block and that is messing things up for you.
Ruby doesn't seem sure about how it should parse this for you. It could be
{render(:xml) => @posts}
or
{render(:xml => @posts)} #throws a different error
In any case, it still thinks it is a Hash.
-Justin