From: "David A. Black" Date: 2007-03-23T21:59:21+09:00 Subject: Re: Question about if in Ruby Hi -- On 3/23/07, Vince H&K wrote: > John Joyce wrote: > > I've seen this in Rails, but it is just ERb, so it is Ruby... > > > > > > <%= link_to 'Previous page', { :page => @product_pages.current.previous > > } if @product_pages.current.previous %> > > > > > > I'm curious how it is different from this... > > > > > > <%= if @product_pages.current.previous link_to ('Previous page', { > > :page => @product_pages.current.previous }) > > end > > %> > > > > > > I've seen this kind of if statement reversal a few times now in Ruby > > code. I call it do-if because it looks similar to do-while, but I am > > curious if reversing the condition affects the interpreter's speed at > > all. If they are the same, then isn't the standard if conditional better > > for human legibility and understanding? > > The first form of the statement is valid for only one instruction. Both > > if something > do_something > end > > and > > do_something if something > > are equivalent, but when it comes to > > if a > one_thing > another_thing > end > > you simply can't use the other form, as it applies to only one instruction. Just for completeness (I'm not recommending it), I'll mention that you can do: one_thing if begin another_thing a_third_thing end David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)