Hi, Ryan,


Thank you very much for your quick reply. I still have problem to 
understand though.

Post the code snippet again here:

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @posts }
    end

My understanding is: respond_to method will take this block as argument, 
within respond_to method, it will call this block code, the 
implementation of respond_to is:

module ActionController
   module MimeResponds
     module InstanceMethods

       def respond_to(*types, &block)
         raise ArgumentError, "respond_to takes either types or a
block, never both" unless types.any? ^ block
         block ||= lambda { |responder| types.each { |type|
responder.send(type) } }
         responder = Responder.new(self)
         block.call(responder)
         responder.respond
       end

       class Responder
         # ...
       end

     end
   end
end

Notice here "block.call(responder)", I believe this "responder" is the 
"format" parameter used in

    do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @posts }
    end

But this doesn't explain only one of format.html or format.xml is 
executed. No matter what object is passed into this block as parameter 
for "format", these two statement will both be executed. Unless, ruby 
implements something like:

   do |format|
     if format == ".html"
        format.html
     if format == ".xml"
        format.xml {render :xml => @posts }

According to what you said: the respond_to method yields
a format object to the block. Once inside the block, the code tells the
format object to perform the default action (render the HTML template)
if the request wants an HTML response. Also, it tells the format object
that, if the request wants an XML response, to execute the given lambda.

I don't see how the code "tells" the format object to perform the 
default action or XML action.

-- 
Posted via http://www.ruby-forum.com/.