# Irb question

**URL:** <https://www.ruby-forum.com/t/irb-question/68960>\
**Category:** Ruby\
**Created:** [September 17, 2006, 1:22pm UTC](https://www.ruby-forum.com/t/irb-question/68960 "2006-09-17T13:22:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![randw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecb155/32.png) [@randw](https://www.ruby-forum.com/u/randw)\
**Post date:** [September 17, 2006, 1:22pm UTC](https://www.ruby-forum.com/t/irb-question/68960/1 "2006-09-17T13:22:55Z")

</div>

Greetings All

I have a question about bindings in the irb when you load a file into  
it. Here is a sample file I tried loading:

def hello  
puts “hello there”  
end  
jello = 42  
puts jello

I load this file from the irb using: load ‘test.rb’  
The response is that it prints out the number 42. So far so good.  
If I type hello to the prompt, I get “hello there” as expected. Also so  
far so good. However, when I type jello I get the following message:

NameError: undefined local variable or method `jello’ for main:Object  
from (irb):2

The question is, why was hello bound and jello not?

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?

Thanks for any help on this in advance.

Rand

---

<div class="post-metadata">

**Author:** ![randw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecb155/32.png) [@randw](https://www.ruby-forum.com/u/randw)\
**Post date:** [September 17, 2006, 2:15pm UTC](https://www.ruby-forum.com/t/irb-question/68960/2 "2006-09-17T14:15:45Z")

</div>

Rand Waltzman wrote:

> I load this file from the irb using: load ‘test.rb’  
> […]  
> NameError: undefined local variable or method `jello’ for main:Object  
> from (irb):2

Local variables are local to a file. Use a constant (Jello) or an  
instance  
variable of the main object (@jello).

Kalman

---

<div class="post-metadata">

**Author:** ![randw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecb155/32.png) [@randw](https://www.ruby-forum.com/u/randw)\
**Post date:** [September 17, 2006, 2:27pm UTC](https://www.ruby-forum.com/t/irb-question/68960/3 "2006-09-17T14:27:13Z")

</div>

Kalman

Thanks. Using a constant worked. Any ideas about the second part of  
the question?

Rand

Kalman N. wrote:

> Rand Waltzman wrote:
> 
> > I load this file from the irb using: load ‘test.rb’  
> > […]  
> > NameError: undefined local variable or method `jello’ for main:Object  
> > from (irb):2
> 
> Local variables are local to a file. Use a constant (Jello) or an  
> instance  
> variable of the main object (@jello).
> 
> Kalman

---

<div class="post-metadata">

**Author:** ![randw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecb155/32.png) [@randw](https://www.ruby-forum.com/u/randw)\
**Post date:** [September 17, 2006, 4:25pm UTC](https://www.ruby-forum.com/t/irb-question/68960/4 "2006-09-17T16:25:57Z")

</div>

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.

Kalman

---

<div class="post-metadata">

**Author:** ![randw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecb155/32.png) [@randw](https://www.ruby-forum.com/u/randw)\
**Post date:** [September 17, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/irb-question/68960/5 "2006-09-17T16:51:54Z")

</div>

Kalman

Thanks for your help. I really appreciate it.

Rand

Kalman N. wrote:

> 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.
> 
> Kalman

---

<div class="post-metadata">

**Author:** ![randw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecb155/32.png) [@randw](https://www.ruby-forum.com/u/randw)\
**Post date:** [September 18, 2006, 10:01am UTC](https://www.ruby-forum.com/t/irb-question/68960/6 "2006-09-18T10:01:14Z")

</div>

Kalman N. schrieb:

> > 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
