If statement and if modifier not equivalent?

M

Mark James

I was under the impression that the if modifier and the
if statememt were equivalent, but for the following code
calling A.test raises an exception, while calling A.test2
succeeds. This is with Ruby 1.8.4.

class A
def A.test
a if a=1
end

def A.test2
if a=1
a
end
end
end
 
S

Sylvain Joyeux

I was under the impression that the if modifier and the
if statememt were equivalent, but for the following code
calling A.test raises an exception, while calling A.test2
succeeds. This is with Ruby 1.8.4.
That's because of the way Ruby decides wether a local variable exists or
not. Basically, a local variable is defined the first time it is assigned
to, so in the first case
def A.test
a if a=1
end
a=1, which defines the local, is seen *after* the 'a' statement. So, 'a'
does not exist and boom

In the second case,
def A.test2
if a=1
a
end
end
a=1 is seen before the 'a' statement, so it works

Regards,
Sylvain
 
M

Mark James

Sylvain said:
That's because of the way Ruby decides wether a local variable exists or
not. Basically, a local variable is defined the first time it is assigned
to, so in the first case


a=1, which defines the local, is seen *after* the 'a' statement. So, 'a'
does not exist and boom

OK, thanks Sylvain. But I would have thought that any examination of
the guarded statement would be delayed until after the condition had
been evaluated. Is a parse like this possible?
 
D

David Vallner

D=C5=88a Nede=C4=BEa 19 Febru=C3=A1r 2006 12:58 Mark James nap=C3=ADsal:
OK, thanks Sylvain. But I would have thought that any examination of
the guarded statement would be delayed until after the condition had
been evaluated. Is a parse like this possible?

Personally, I'd be surprised if the parser paid any attention to much of th=
e=20
semantics of the parsed text at all - that's what the interpreter is suppos=
ed=20
to do. The variable declaration checking at parse-time seems to be the=20
exception here.

Syntax ge... gurus, feel free to elaborate.

David Vallner
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: If statement and if modifier not equivalent?"
|D=C5=88a Nede=C4=BEa 19 Febru=C3=A1r 2006 12:58 Mark James nap=C3=ADsal:
|> OK, thanks Sylvain. But I would have thought that any examination of
|> the guarded statement would be delayed until after the condition had
|> been evaluated. Is a parse like this possible?
|
|Personally, I'd be surprised if the parser paid any attention to much of t=
he=20
|semantics of the parsed text at all - that's what the interpreter is suppo=
sed=20
|to do. The variable declaration checking at parse-time seems to be the=20
|exception here.
|
|Syntax ge... gurus, feel free to elaborate.

Agreed with you. But there's a vague possibility that this issue will
be solved in Ruby 2.0 in different manner.

matz.
 
G

Garance A Drosehn

------=_Part_7224_6946399.1140402222112
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I was under the impression that the if modifier and the
if statememt were equivalent, but for the following code
calling A.test raises an exception, while calling A.test2
succeeds. This is with Ruby 1.8.4.

class A
def A.test
a if a=3D1
end

def A.test2
if a=3D1
a
end
end
end
Do you really want 'if a =3D 1' there, and not 'if a =3D=3D 1'?

--
Garance Alistair Drosehn =3D (e-mail address removed)
Senior Systems Programmer
Rensselaer Polytechnic Institute; Troy, NY; USA

------=_Part_7224_6946399.1140402222112--
 
G

Gregory Brown

Do you really want 'if a =3D 1' there, and not 'if a =3D=3D 1'?

yes. I imagine so.

in a more complicated example

if ( a =3D get_something_that_might_be_nil_or_false )
puts a
end
 
M

matt.smillie

Which makes the assignment a side-effect of the conditional, and
depending on the context can be an excellent example of bad
side-effects, particularly from a software-engineering point of view,
where the code has to be maintained over any sort of time, by a team.

I think of it as the imperative corollary to duck-typing: if it looks
like a duck and sounds like a duck, it should *be* a duck.

There's a great public example of where this technique can break down
from an attempted hack on the linux kernal a couple of years ago. It's
worth noting that it was only caught because of 1) very tight, very
paranoid peer review, and 2) the secure nature of the project. It
would have gone through unnoticed virtually anywhere else. In my past
life doing telecoms programming, I can remember this exact pattern
being responsible for bringing down a good portion of the US East
Coast's 1-800 (and 900, etc) phone numbers.

To each their own, and if it's code for your own use it's unlikely to
matter, but assignment-as-conditional really, really smells bad to me.

http://kerneltrap.org/node/1584
+ if ((options == (__WCLONE|__WALL)) && (current->uid = 0))
+ retval = -EINVAL;

Associated commentary:
 
D

David Vallner

D=C5=88a Pondelok 20 Febru=C3=A1r 2006 14:53 (e-mail address removed) nap=C3=
=ADsal:
Ow.

David Vallner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top