On Wed, Oct 31, 2012 at 4:28 PM, Eliezer Croitoru <[email protected]>wrote:

> I am wondering who will use such if condition?
> it's not even a if condition at all.
> why would you declare the variable in the if statement?
>

That's a good question.  Generally assignments in conditions of control
flow statements only make sense for loops because that sometimes allows to
avoid redundant code:

# option 1: redundant code
line = io.gets

while line
  puts line
  line = io.gets
end

# option 2: no redundant code and shorter
while line = io.gets
  puts line
end

# even better
io.each_line do |line|
  puts line
end

With an "if" it's never necessary.  And I do not think it makes a
difference performance wise.

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

-- You received this message because you are subscribed to the Google Groups 
ruby-talk-google 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 https://groups.google.com/d/forum/ruby-talk-google?hl=en

Reply via email to