Yeah, great point. How to know how deep to go. I was looking at some of the Ruby global vars:
http://www.rubyist.net/~slagell/ruby/globalvars.html This one is similar: $n the nth subexpression in the last match (same as $~[n]) But it seems somehow not so clean. We have this nifty idiom: x ||= "hello" But I can't think of a neat syntax for what I'm wanting. On Feb 14, 5:45 pm, Philip Hallstrom <[email protected]> wrote: > > I was looking at this bit of code within Rails: > > > # File actionpack/lib/action_controller/metal/flash.rb, line 12 > > def redirect_to(options = {}, response_status_and_flash = {}) #:doc: > > if alert = response_status_and_flash.delete(:alert) > > flash[:alert] = alert > > end > > > if notice = response_status_and_flash.delete(:notice) > > flash[:notice] = notice > > end > > > if other_flashes = response_status_and_flash.delete(:flash) > > flash.update(other_flashes) > > end > > > super(options, response_status_and_flash) > > end > > > And I thought: ouch, aren't those supposed to be double-equals? Well, > > no, it's an assignment within an if statement. It saves a line of > > code per usage, thus three lines of code in that method. > > > But I thought: wow, that sure violates the principle of least > > surprise. Perhaps it would be nice if there were a way to reference > > the most recent if-expression. I recall how a lone underscore in irb > > will contain the value of whatever was last evaluated. So perhaps > > something like this would be nice: > > > if response_status_and_flash.delete(:notice) > > flash[:notice] = _ > > end > > > instead of what we see above: > > > if notice = response_status_and_flash.delete(:notice) > > flash[:notice] = notice > > end > > Interesting, but I think using _ is even more surprising isn't it? What > happens when you have this... > > if something-that-returns-a-value-and-passes-the-if-test > if something-that-returns-a-value-but-fails-the-if-test > # never happens > else > # what does _ reference? The very top level if? Or the second one? > # Both were evaluated, but one failed so does it count towards _ or not? > # How do I access the first _ if Ruby decides _ is the second one? > end > end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

