# Why is the Binding object so useless?

**URL:** <https://www.ruby-forum.com/t/why-is-the-binding-object-so-useless/182272>\
**Category:** Ruby\
**Created:** [January 25, 2010, 9:57pm UTC](https://www.ruby-forum.com/t/why-is-the-binding-object-so-useless/182272 "2010-01-25T21:57:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![murfhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d26b3c/32.png) [@murfhy](https://www.ruby-forum.com/u/murfhy)\
**Post date:** [January 25, 2010, 9:57pm UTC](https://www.ruby-forum.com/t/why-is-the-binding-object-so-useless/182272/1 "2010-01-25T21:57:42Z")

</div>

I am used to Ruby being a rather reflective language that gives carte  
blanche to the programmer. So I found this rather surprising:

> > Marshal.dump binding  
> > TypeError: no marshal\_dump is defined for class Binding

Whaa?

> > Binding.new  
> > NoMethodError: undefined method `new’ for Binding:Class

Huh?

> > binding.methods(false)  
> > =\> []

You’re kidding me, right?

I would expect you to be able to do all of the above. In addition to  
that,  
it’s sure be nice to do some of the following:

> > Binding.new :a =\> 1, :b =\> 2, :c =\>3  
> > =\> #Binding:0x1018130b0 # new binding object with the given variables  
> > bound
> 
> > binding.to\_hash  
> > =\> {:a =\> 1, :b =\> 2, :c =\> 3} # Bound variables, as a hash
> 
> > binding.to\_yaml  
> > =\> “— !ruby/object:Binding \na: 1\nb: 2\nc: 3\n” # YAML dump of a  
> > Binding  
> > object

And hey, maybe Binding#inspect could give a little more information than  
its  
object\_id?

Canned responses to apologists:

= Why do you want to do that?

I don’t know, why should you be able to redefine Integer#+?

= No really, why do you want to do that?

I would like to implement a remote REPL with a stateless Ruby server and  
a  
non-Ruby client

= Just use DRb!

I am really not wanting to open up an addition port just for DRb nor  
pursue  
solutions which involve a non-Ruby client talking the DRb protocol.  
Embedding some form of Ruby interpreter client side is not an option.

Props to Rubinius for fixing some of these problems!

irb(main):001:0\> Binding.new  
=\> #Binding:0x2e0

Yay! A clean binding!

> > Marshal.dump Binding.new  
> > =\> “\004\bo:\fBinding\000”

Amazing!

---

<div class="post-metadata">

**Author:** ![murfhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d26b3c/32.png) [@murfhy](https://www.ruby-forum.com/u/murfhy)\
**Post date:** [January 26, 2010, 3:40pm UTC](https://www.ruby-forum.com/t/why-is-the-binding-object-so-useless/182272/2 "2010-01-26T15:40:28Z")

</div>

Tony A. wrote:

> = No really, why do you want to do that?
> 
> I would like to implement a remote REPL with a stateless Ruby server and  
> a  
> non-Ruby client

Your proposed feature would also make things like the ActionPack  
template rendering much cleaner. grep for “local\_assigns” in  
action\_view/renderable.rb (rails 2.3.x) and weep. Here is what they end  
up doing, where local\_assigns is a hash of variable=\>value bindings:

```
  def compile!(render_symbol, local_assigns)
    locals_code = local_assigns.keys.map { |key| "#{key} = 

```

local\_assigns[:#{key}];" }.join

```
    source = <<-end_src
      def #{render_symbol}(local_assigns)
        old_output_buffer = 

```

output\_buffer;#{locals\_code};#{compiled\_source}  
ensure  
self.output\_buffer = old\_output\_buffer  
end  
end\_src

(Rails 3 is the same, but the file is action\_view/template.rb and the  
method is compile not compile!)

The above is probably what your REPL server should do.

I guess what you propose wasn’t implemented simply because it involves  
work to convert bindings between their internal representation and  
external forms like Hash and Marshal#dump. If you grab an existing live  
binding and pass it around, the conversion isn’t required.

---

<div class="post-metadata">

**Author:** ![murfhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d26b3c/32.png) [@murfhy](https://www.ruby-forum.com/u/murfhy)\
**Post date:** [January 26, 2010, 4:08pm UTC](https://www.ruby-forum.com/t/why-is-the-binding-object-so-useless/182272/3 "2010-01-26T16:08:13Z")

</div>

2010/1/25 Tony A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I am used to Ruby being a rather reflective language that gives carte  
> blanche to the programmer. So I found this rather surprising:
> 
> > > Marshal.dump binding  
> > > TypeError: no marshal\_dump is defined for class Binding
> 
> Whaa?

I don’t think it does make sense to serialize a binding for the same  
reason that you cannot create a new binding: a binding is bound to a  
particular program context - I assume this also includes caller stack  
and other internal state.

Even if a binding contains local variables what is the effect of  
deserializing a binding? If your local variables get updated that is  
certainly not a good idea. If you want to serialize state use objects  
with the proper state.

> > > Binding.new  
> > > NoMethodError: undefined method `new’ for Binding:Class
> 
> Huh?

A binding is not a regular object but it is a handle on the state of  
the runtime. A method object cannot be serialized as well because  
that would mean that the original code would have to be written (and  
read); this also has security implications (in the case of Method).

But if you have to\_hash you can serialize that.

> I would expect you to be able to do all of the above. In addition to that,  
> it’s sure be nice to do some of the following:
> 
> > > Binding.new :a =\> 1, :b =\> 2, :c =\>3  
> > > =\> #Binding:0x1018130b0 # new binding object with the given variables  
> > > bound

As above.

> > > binding.to\_hash  
> > > =\> {:a =\> 1, :b =\> 2, :c =\> 3} # Bound variables, as a hash  
> > > binding.to\_yaml  
> > > =\> “— !ruby/object:Binding \na: 1\nb: 2\nc: 3\n” # YAML dump of a Binding  
> > > object

Same as above for Marshal.

> And hey, maybe Binding#inspect could give a little more information than its  
> object\_id?

Yeah, #to\_hash and #inspect sound pretty good. You might even add a  
method #update receiving a Hash which is interpreted as a mapping from  
local variables to values and which overwrites the current binding.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![murfhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d26b3c/32.png) [@murfhy](https://www.ruby-forum.com/u/murfhy)\
**Post date:** [January 26, 2010, 8:11pm UTC](https://www.ruby-forum.com/t/why-is-the-binding-object-so-useless/182272/4 "2010-01-26T20:11:45Z")

</div>

On Tue, Jan 26, 2010 at 8:07 AM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> reason that you cannot create a new binding: a binding is bound to a  
> particular program context - I assume this also includes caller stack  
> and other internal state.

Why can’t bindings be independently garbage collected objects too? (as  
it  
appears they are in rbx)

> Even if a binding contains local variables what is the effect of  
> deserializing a binding? If your local variables get updated that is  
> certainly not a good idea. If you want to serialize state use objects  
> with the proper state.

The goal is to have an object to pass to eval as the binding argument,  
not  
to update locals.

Ideally I could pass a fresh binding (made with Binding.new) to eval,  
and  
when eval is finished it will be updated with whatever assignments were  
made  
in the evaluated code. I could then serialize this and persist it until  
the  
next eval call.
