From: Josh Cheek Date: 2011-04-05T12:30:47+09:00 Subject: Re: pipe question --0016e68def6c5a4b6604a0238053 Content-Type: text/plain; charset=ISO-8859-1 On Mon, Apr 4, 2011 at 7:12 PM, 7stud -- wrote: > Josh Cheek wrote in post #990762: > > > > 3) For some reason that I don't know (probably interpreter magic) an > > uninitialized variable can be referenced in a boolean equation and it > > will > > evaluate to nil. > > > > first_name = first_name || "" # => "" > > first_name = "josh" > > first_name = first_name || "" # => "josh" > > > > > > So, it will set the variable to the empty string, if the variable is > > undeclared, or false , or nil. > > I think the story goes something like this: when the parser sees any > 'name =' expression, name gets entered into the symbol table. > Thereafter, you will no longer get an exception when referencing the > variable. In your code, the parser sees 'first_name =', so first_name > is entered into the symbol table, and then when ruby executes your code, > the expression on the right hand side of the equals sign is executed. > > Makes sense, and passed the test I constructed to invalidate it: lhs = rhs || "pass" lhs # => # ~> -:1:in `
': undefined local variable or method `rhs' for main:Object (NameError) I probably never figured that out because it depends on the variable type, for example: lhs = @rhs || "pass" # => "pass" and lhs = $rhs || "pass" # => "pass" > Here is an example that might prove illustrative: > > if 1 > 10 > x = 'hello' #never executes > end > > puts x #=> nil > puts y #=> undefined local variable or method `y' > #for main:Object (NameError) > > Good thought. I'm honestly surprised that works! I guess it must add x to the symbol table at parse time. --0016e68def6c5a4b6604a0238053--