[Rails] Re: An idea for new syntactic sugar in Ruby
I also found this on stackoverflow:
http://stackoverflow.com/questions/3835170/ruby-assignment-and-comparison-in-one-line
On Feb 14, 5:18 pm, Kai Middleton <kai.middle...@gmail.com> 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
--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home