Pit Capitain
9/18/2006 8:01:00 AM
Kalman Noel schrieb:
> Rand Waltzman:
>> def hello
>> puts "hello there"
>> end
>> [...]
>> Another interesting phenomenon occurs if I type "hello = 42" to the
>> interpreter, i.e., I try to rebind hello. That seems to work fine as
>> well. If I type hello, I get 42 as expected. However, if I try to
>> reenter the definition of hello as I had it in the original file and try
>> typing hello again, I still get 42. The question is, why didn't it
>> allow me to rebind hello?
>
> In Ruby, methods and local variables live in different parts of the world.
> The current object can have a method with the same name as a local
> variable. In such cases, the interpreter supposes that the programmer
> forgot about the method and wants the variable.
Note also, that you can still call the method, if there are parentheses
or parameters passed to the method:
def a; "method"; end
a = "local variable"
puts a # => local variable
puts a() # => method
Regards,
Pit