# Bindings and Context

**URL:** <https://www.ruby-forum.com/t/bindings-and-context/145563>\
**Category:** Ruby\
**Created:** [August 11, 2008, 4:59pm UTC](https://www.ruby-forum.com/t/bindings-and-context/145563 "2008-08-11T16:59:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [August 11, 2008, 4:59pm UTC](https://www.ruby-forum.com/t/bindings-and-context/145563/1 "2008-08-11T16:59:59Z")

</div>

Hi,  
I’m still really confused about Ruby Closures.

At first I thought closures are just blocks who “remember” the context  
that they were defined in (so that they can refer to local variables and  
methods)

And then I discovered instance\_eval (which changes the context that a  
block is evaluated in).

When you use instance\_eval you can’t call local methods anymore  
(because, the scope has changed, and those methods aren’t local  
anymore).

But how come I can still use local variables?

```
myStr = "myString"
o = Object.new
o.instance_eval do
  @str = myStr # <- why does this still work?
  def method
    puts @str
  end
end

```

So are local variables sort of an exception to instance\_eval? And in the  
above example, how come it won’t work anymore if I change myStr into an  
instance variable @myStr?

Thanks a lot for helping  
-Patrick

PS: Is there a detailed document I can read about OO languages? I come  
from Java and I’m feeling a little lost amidst all these advanced OO  
features.

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [August 11, 2008, 5:07pm UTC](https://www.ruby-forum.com/t/bindings-and-context/145563/2 "2008-08-11T17:07:17Z")

</div>

On Monday 11 August 2008, Patrick Li wrote:

> When you use instance\_eval you can’t call local methods anymore  
> puts @str  
> end  
> end
> 
> So are local variables sort of an exception to instance\_eval? And in the  
> above example, how come it won’t work anymore if I change myStr into an  
> instance variable @myStr?

instance\_eval doesn’t change scope. Its only effect is to change the  
value of  
the special local variable self. This explains why you can’t access  
instance  
variables anymore: with self changed, the instance variables are  
instance  
variables of the new self (that is, the receiver of instance\_eval). The  
same  
is true for instance methods. Local variables, on the other hand, are  
unaffected by the change of self and so you can still use them.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [August 11, 2008, 5:12pm UTC](https://www.ruby-forum.com/t/bindings-and-context/145563/3 "2008-08-11T17:12:04Z")

</div>

Ah. okay.  
That really helps.

So is this a correct interpretation?

Every block/closure has an associated binding.

The binding can be loosely described as  
-a list of local variables  
-and self.

Calls to instance variables are shorthand for “self.@myInstanceVariable”  
and calls to instance methods are shorthand for “self.myInstanceMethod”

Does that sound alright?  
-Cuppo

PS: You’ve really helped me out a lot recently Stefano. Thanks a lot.

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [August 11, 2008, 6:51pm UTC](https://www.ruby-forum.com/t/bindings-and-context/145563/4 "2008-08-11T18:51:04Z")

</div>

On Monday 11 August 2008, Patrick Li wrote:

> Ah. okay.  
> That really helps.
> 
> So is this a correct interpretation?
> 
> Every block/closure has an associated binding.
> 
> The binding can be loosely described as  
> -a list of local variables  
> -and self.

Yes, I think so

> Calls to instance variables are shorthand for “self.@myInstanceVariable”  
> and calls to instance methods are shorthand for “self.myInstanceMethod”
> 
> Does that sound alright?

that’s perfectly correct for calls to instance methods. For instance  
variables, it’s true only conceptually, since ruby doesn’t allow to call  
them  
thay way.

Stefano

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [August 11, 2008, 6:55pm UTC](https://www.ruby-forum.com/t/bindings-and-context/145563/5 "2008-08-11T18:55:03Z")

</div>

Thank you Stefano.  
That clears up a lot of my misconceptions.  
-Patrick

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [August 11, 2008, 7:00pm UTC](https://www.ruby-forum.com/t/bindings-and-context/145563/6 "2008-08-11T19:00:27Z")

</div>

On 11.08.2008 17:09, Patrick Li wrote:

> So is this a correct interpretation?
> 
> Every block/closure has an associated binding.
> 
> The binding can be loosely described as  
> -a list of local variables  
> -and self.

Yep.

> Calls to instance variables are shorthand for “self.@myInstanceVariable”  
> and calls to instance methods are shorthand for “self.myInstanceMethod”

Yes, apart from access control:

robert@fussel ~  
$ ruby -e ‘class F;private;def x;puts 111;end  
end;f=F.new;f.instance\_eval { x };f.x’  
111  
-e:1: private method `x’ called for #\<F:0x1002eef4\> (NoMethodError)

“private” disallows method invocation with explicit receiver.

Kind regards

robert
