From: Robert Klemme Date: 2012-11-01T00:48:58+09:00 Subject: Re: bug?: local variable created in if modifier not available in modified expression --f46d042f969825780004cd5cd7ed Content-Type: text/plain; charset=ISO-8859-1 On Wed, Oct 31, 2012 at 4:28 PM, Eliezer Croitoru 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/ --f46d042f969825780004cd5cd7ed Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable


On Wed, Oct 3= 1, 2012 at 4:28 PM, Eliezer Croitoru <eliezer@ngtech.co.il> wrote:
I am wondering who will use such if conditio= n?
it's not even a if condition at all.
why would you declare the variable in the if statement?

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

# option 1: redundan= t code
line =3D io.gets

while line
=A0 puts line
=A0 line =3D io.gets=
end

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

# even better
io.each_line do |li= ne|
=A0 puts line
end

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

Kind re= gards

robert

--
remember.guy do |as, often| as.you_can - = without end
http://blog.rubybestpractice= s.com/
--f46d042f969825780004cd5cd7ed--