J
Jon Leighton
Hi,
So it turns out the following results in a NoMethodError:
foo if foo = 1
I.e. the assignment to foo in the conditional is not in-scope when the
'then' part is evaluated.
Obviously the following does not result in a NoMethodError:
if foo = 1
foo
end
I would expect them to work the same way. Does anybody know the reason
for this? How exactly is the first example parsed?
Secondly, suppose foo has not yet been assigned. The following results
in foo being assigned to nil:
foo = 1 if false
In fact, so does:
if false
foo = 1
end
and even:
if false
foo = 1
bar = 2
end
(results in both foo and bar being assigned nil)
Also consider:
foo = 1
foo = 2 if false
This results in foo keeping its value of 1, not being assigned to nil.
So it is not simply the case that the second statement is parsed as:
foo = (2 if false)
Is anyone able to elaborate on what exactly the interpreter is doing in
these cases?
Thanks
So it turns out the following results in a NoMethodError:
foo if foo = 1
I.e. the assignment to foo in the conditional is not in-scope when the
'then' part is evaluated.
Obviously the following does not result in a NoMethodError:
if foo = 1
foo
end
I would expect them to work the same way. Does anybody know the reason
for this? How exactly is the first example parsed?
Secondly, suppose foo has not yet been assigned. The following results
in foo being assigned to nil:
foo = 1 if false
In fact, so does:
if false
foo = 1
end
and even:
if false
foo = 1
bar = 2
end
(results in both foo and bar being assigned nil)
Also consider:
foo = 1
foo = 2 if false
This results in foo keeping its value of 1, not being assigned to nil.
So it is not simply the case that the second statement is parsed as:
foo = (2 if false)
Is anyone able to elaborate on what exactly the interpreter is doing in
these cases?
Thanks