# What to use binding for?

**URL:** <https://www.ruby-forum.com/t/what-to-use-binding-for/100273>\
**Category:** Ruby\
**Created:** [July 28, 2006, 2:54am UTC](https://www.ruby-forum.com/t/what-to-use-binding-for/100273 "2006-07-28T02:54:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [July 28, 2006, 2:54am UTC](https://www.ruby-forum.com/t/what-to-use-binding-for/100273/1 "2006-07-28T02:54:41Z")

</div>

Hi everyone,

There was a post a couple of days ago that included a call to binding

I was interested in this and went to look up the binding docs. All  
good, it  
seems simple enough, but I can’t think of how to use it.

Could anyone provide a simple explaination/code of an example of how it  
might be used.

Thanx

Daniel

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [July 28, 2006, 7:41am UTC](https://www.ruby-forum.com/t/what-to-use-binding-for/100273/2 "2006-07-28T07:41:11Z")

</div>

Daniel N wrote:

> Hi everyone,
> 
> There was a post a couple of days ago that included a call to binding
> 
> I was interested in this and went to look up the binding docs. All good, it  
> seems simple enough, but I can’t think of how to use it.
> 
> Could anyone provide a simple explaination/code of an example of how it  
> might be used.

Google say:

[http://extensions.rubyforge.org/rdoc/classes/Binding.html](http://extensions.rubyforge.org/rdoc/classes/Binding.html)  
[http://groups.google.com/group/comp.lang.ruby/browse\_frm/thread/1d08d0cdb3b53d11](http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/1d08d0cdb3b53d11)

[http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc/style/print](http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc/style/print)

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [July 28, 2006, 9:45pm UTC](https://www.ruby-forum.com/t/what-to-use-binding-for/100273/3 "2006-07-28T21:45:38Z")

</div>

Gene T. wrote:

> > might be used.
> 
> Google say:
> 
> [http://extensions.rubyforge.org/rdoc/classes/Binding.html](http://extensions.rubyforge.org/rdoc/classes/Binding.html)  
> [http://groups.google.com/group/comp.lang.ruby/browse\_frm/thread/1d08d0cdb3b53d11](http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/1d08d0cdb3b53d11)
> 
> [http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc/style/print](http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc/style/print)

Looking at Jim W.'s article on writing a local variable swap method  
using binding, I couldn’t help but think that the following would reduce  
code size, complexity, and call keystrokes in the final embodiment of  
the method:

def swap(a, b, bind)  
eval “#{a}, #{b} = #{b}, #{a}”, bind  
end

a = Object.new #=\> #Object:0x2c64540  
b = Object.new #=\> #Object:0x2c617c8

swap(:a, :b, binding)

a #=\> #Object:0x2c617c8  
b #=\> #Object:0x2c64540

Not that I have any delusions about this pedagogical method having any  
relevance in the real world, but there it is just the same.

Tom

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [July 29, 2006, 2:58am UTC](https://www.ruby-forum.com/t/what-to-use-binding-for/100273/4 "2006-07-29T02:58:22Z")

</div>

On 7/28/06, Tom W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > seems simple enough, but I can’t think of how to use it.
> 
> end  
> a = Object.new #=\> #Object:0x2c64540  
> Tom
> 
> –  
> Tom W.  
> Helmets to Hardhats  
> Software Developer  
> removed\_email\_address@domain.invalid  
> [www.helmetstohardhats.org](http://www.helmetstohardhats.org)
> 
> Thanx guys. This has helped me see how to use binding. Although I’m  
> still not sure when to use it 🙂
