Summercool Summercool wrote: > So at first I thought a Ruby program is interpreted? If interpreted, > then the interpreter will see the "a = 1" at first as treat "a" as a > variable, and then the second time it sees "a", the interpreter should > treat it as a variable again, not as a method. So does that mean a > Ruby program is not interpreted but compiled into some bytecode first? > But then, sometimes I run a Ruby program and then it can run all the way > until it see an "undefined local variable"... so that means it probably > is not compiled... or else it would have stopped without running > anything at all. > > So is a Ruby program interpreted or compiled? It is interpreted but before being interpreted it is parsed. And whether a symbol is a variable or not is determined at parse time: when the parser sees the assignment "a = 1", it assumes that 'a' is a variable for all subsequent *lines* of the source code, until the end of the method or block where the assignment occured. It is has nothing to do with the order in which the code is executed. Also you'll notice that the "undefined local variable" message is actually "undefined local variable or method", because ruby can't know if the unknown symbol was supposed to be a method or a variable. Daniel