Trans
3/24/2007 11:53:00 AM
On Mar 23, 7:53 am, Vince H&K <vince.h...@gmail.com> 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.
There is one other difference.
if a = 1
p a
end
p a if a = 1
The later does not assign the a.
btw you can also use parens for more than one call.
( do_this ; do_that ) if something
or
( do_this
do_that
) if something
T.