"Zehao" <zehao.chen / gmail.com> schrieb im Newsbeitrag 
news:1108274165.071278.164610 / z14g2000cwz.googlegroups.com...
> One interesting thing is when running the following codes
>
> File.open("testfile") |aFile| do
>   print a while a=aFile.gets
> end
>
> one will get an error indicating a doesn't exist. This error can be
> fixed by adding a line a="" before the print command.
>
> But logically speaking, "print a" should be interpreted after
> "a=File.gets". Is this because the interpreter couldn't find the object
> "a" when first scanning the second line?

I suspect it's the same issue as with local variables: Ruby applies a 
certain heuristic to determine whether something is a local var or not. 
This heuristic is based on *syntactical* order of occurrence

>> def foo
>> print a
>> a = 10
>> end
=> nil
>> foo
NameError: undefined local variable or method `a' for main:Object
        from (irb):2:in `foo'
        from (irb):5

See also
http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UO
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#S3

Kind regards

    robert