Iñaki Baz Castillo
3/29/2008 11:42:00 PM
El Domingo, 30 de Marzo de 2008, Tim Hunter escribi=C3=B3:
> In case (b), there has not yet been an assignment before the reference
> to a, so Ruby assumes that "a" is a method call, yet there is no
> definition for a so you get the message.
b)
a if a=3D"QWEQWE"
NameError: undefined local variable or method `a' for main:Object
But why the first "a" is interpreted before the "if" stament? Imagine this=
=20
example:
defined?a
=3D> nil
a=3D"BLABLA" if 2 =3D=3D 3
=3D> nil
a
=3D> nil
After this code "a" will remain "nil" (or the previous value it had), so:
a=3D"BLABLA"
hasn't been interpreted since the "if" stament failed.
Also note this other example:
=20
defined?kk
=3D> nil
kk if nil
=3D> nil
Why in this last case the code doesn't return:
"undefined local variable or method kk' "
?
I assume the reason: "kk" is not interpreted if the condition fails.
In b) the first "a" is then interpreted AFTER the condition success, so in=
=20
that moment "a" has been already declared (into the condition), so it would=
=20
exist and not give an error.
Am I not right?
=2D-=20
I=C3=B1aki Baz Castillo