From: Todd Benson Date: 2008-01-18T09:01:47+09:00 Subject: Re: Ruby syntax in "respond_to do |format| line -- can clarify? On Jan 17, 2008 11:34 AM, Andrew Timberlake wrote: > Joshua > > respond_to is the method, it provides the block with the format object. > Within the block, you are calling the methods html and xml on the format object supplied. > Rather than a loop, the block is more like a callback function that accepts a format object as a parameter. > The format object now understands that it can respond with either html or xml output. > Rails then uses the Accepts header of the calling client (browser etc) to decide which of the formats to return. > You can also append .html or .xml to the url to force one or the other format. > > Hope that helps > > Andrew Timberlake > andrew@andrewtimberlake.com > 082 415 8283 > skype: andrewtimberlake > > "I have never let my schooling interfere with my education." > --Mark Twain To illustrate this in a _very_ simple way... # the following method is akin to what is already defined by rails (#respond_to, which is much more complicated, of course) def foo( &block ) block.call( 42 ) end # the code you execute foo do |number| # 42 is thrown into your block puts number puts number * 2 end In a similar way, the object named "format" (or whatever you want to name it) is thrown into your block by the framework. At least, that's how I think rails handles it. I've never used the method recently, myself, and unfortunately I don't have rails handy at the moment. Todd