From: Robert Klemme Date: 2010-01-27T00:07:48+09:00 Subject: Re: Why is the Binding object so useless? 2010/1/25 Tony Arcieri : > 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 > => # # 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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/