Hi --

On Mon, 17 Sep 2007, Summercool Summercool wrote:

> the following program for showing Variable / Method Ambiguity
>
>  def a
>    print "Function 'a' called\n"
>    99
>  end
>
>  for i in 1..2
>    if i == 2
>      print "a=", a, "\n"
>    else
>      a = 1
>      print "a=", a, "\n"
>    end
>  end
>
> will produce the result:
>
> a=1
> Function 'a' called
> a=99
>
> (as described in the PickAx2 book, p. 329)
>
> 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.

No, because there's no such variable in scope. The second time through
the loop, there's no assignment to a, and the first variable a has
gone out of scope. So the only thing that "a" could mean, outside of
an assignment, is the method a.


David

> 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?
> -- 
> Posted via http://www.ruby-forum.com/.
>

-- 
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
   RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
     & consulting:  Ruby Power and Light, LLC (http://www.rubypal.com)