What to use binding for?

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

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://groups.google.com/group/comp.lang.ruby/browse_frm/thread/1d08d0cdb3b53d11

http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc/style/print

Gene Tani wrote:

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://groups.google.com/group/comp.lang.ruby/browse_frm/thread/1d08d0cdb3b53d11

http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc/style/print
  
Looking at Jim Weirich'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

···

--
Tom Werner
Helmets to Hardhats
Software Developer
tom@helmetstohardhats.org
www.helmetstohardhats.org

Gene Tani wrote:
> 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://groups.google.com/group/comp.lang.ruby/browse_frm/thread/1d08d0cdb3b53d11
>
> http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc/style/print
>

Looking at Jim Weirich'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

--
Tom Werner
Helmets to Hardhats
Software Developer
tom@helmetstohardhats.org
www.helmetstohardhats.org

Thanx guys. This has helped me see how to use binding. Although I'm

still not sure when to use it :slight_smile:

···

On 7/28/06, Tom Werner <tom@helmetstohardhats.org> wrote: