From: MaggotChild on
I thought this was odd:

def bs
1
end

print y + 1 if y = bs

NameError: undefined local variable or method `y' for main:Object

And, of course, the multi-line if works:

if x = bs
print x + 1
end
From: Eric Christopherson on
On Wed, Mar 31, 2010 at 4:05 PM, MaggotChild <hsomob1999(a)yahoo.com> wrote:
> I thought this was odd:
>
> def bs
>  1
> end
>
>  print y + 1 if y = bs
>
> NameError: undefined local variable or method `y' for main:Object
>
> And, of course, the multi-line if works:
>
> if x = bs
>  print x + 1
> end

Something like this came up a few weeks ago. I think the parser is the
one generating the NameError, because the parser only consider a
variable to be defined if it sees an assignment to it, and such
assignment has to come *before* any use of the variable. Even though
the interpreter actually would actually execute y = bs first, the
parser doesn't see it at the point where its value is used.

From: Benoit Daloze on
[Note: parts of this message were removed to make it a legal post.]

> Something like this came up a few weeks ago. I think the parser is the
> one generating the NameError, because the parser only consider a
> variable to be defined if it sees an assignment to it, and such
> assignment has to come *before* any use of the variable. Even though
> the interpreter actually would actually execute y = bs first, the
> parser doesn't see it at the point where its value is used.
>

Yeah, that's a bit annoying and considerably reduce while-one-liners
possibilities:

lines = IO.read('mac.txt').lines.to_a

puts line while line = lines.pop

but that works if you assign anything to line before ...