From: Robert Klemme Date: 2007-03-16T18:15:07+09:00 Subject: Re: Anyone playing with higher order messaging in ruby? On 15.03.2007 21:28, Rick DeNatale wrote: > 2) mime content negotiation > > An action might be able to respond to a request with more than one > mime-type. An HTTP request can give a prioritized lists of mime-types > which the client will accept. The way to handle this negotiation in > an action is to use the respond_to method you might have code in an > action like: > > respond_to do |wants| > wants.html { redirect_to(person_list_url) } > wants.js > wants.xml { render :xml => @person.to_xml(:include => > @company) } > end > > What this code says is something like: > if the requester wants html redirect to the person_list_url > if the requester wants java script perform the normal rendering > for javascript (such as look for an rjs template with the right name > and render that) > etc. > > It's kind of like a case statement, except what really happens also > depends on which of those alternatives came first in the requests list > of acceptable types. That is no difference to case statements: with those, order matters as well: >> x="foo" => "foo" >> case x >> when /fo/; puts 1 >> when /oo/; puts 2 >> else puts 0 >> end 1 => nil >> case x >> when /oo/; puts 2 >> when /fo/; puts 1 >> else puts 0 >> end 2 => nil I think the major difference (apart from syntactical aspects) is the execution time. Of course this could also be mimicked with a case statement by returning a Proc but the Rails version looks much more elegant. > It's good to read such carefully crafted code and understand the > techniques and the possibilities, but I'd caution about getting too > carried away with such things before you are ready, and most folks > thing they are ready before they really are. Agreed. ;-) Kind regards robert