On Jun 28, 2006, at 1:36 PM, Mr Beev wrote: > irb> puts(line) while line = gets > this is test line > [EOF] > NameError: undefined local variable or method `line' for main:Object > from (irb):1 > > The Pickaxe says on page 354 that unless the expression (on the > left of > the while) is not a block, then the loop gets executed ZERO or more > times. So it has to evaluate the condition BEFORE the expression, > which > ought to assign a value to "line" (thus defining it). > > This will work, but shouldn't be necessary: > > line = nil > puts(line) while line = gets > > > Any insight would be much appreciated!!! > > -- > Posted via http://www.ruby-forum.com/. > Erik already answered but I'll throw in my two cents. The first assignment of a local variable also declares it. What this means is: puts(line) while (declare line; line = gets) line is not in scope for puts(line) yet. Ruby creates it's lexical scopes for local vars by looking top to bottom, left to right at _parse_ time.