From: Intransition Date: 2010-06-11T08:25:28+09:00 Subject: Re: Why private #binding? On Jun 10, 6:45 pm, Rick DeNatale wrote: > On Thu, Jun 10, 2010 at 5:08 PM, Yukihiro Matsumoto wrote: > > Hi, > > > In message "Re: Why private #binding?" > >    on Fri, 11 Jun 2010 04:57:32 +0900, Intransition writes: > > > |It doesn't recognize it's receiver? > > > binding returns the binding object that has info on > > >  * the receiver > >  * local variables > >  * etc. > > > but invoking it from public form does not explain the context except > > for the receiver. > > >                                                        matz. > > Yes, actually my initial thought about self was the only thing that a > non-private binding method would get 'right'. > > The real issue, I think, is that objects in general don't have > bindings, invocations do. Making Kernel#binding private means that > unless you resort to trickery, it will only occur in the context of an > invocation. > > Here's a better example: > > def test(code) >   x = 1 >   b = "abc".send(:binding) >   eval(code, b) > end > > test("self") # => "abc" > test("x")    # => 1 > > The variable x has nothing to do with the string "abc". > > "abc".instance_eval  is a better choice if you want to bind self in an > evaluated string or block. But ERB needs a binding. And in almost every use of binding I have ever had it eds up coming from an external object. After all, it is rare one would need to use the binding from within the binding. I will often I make quick use of such a binding --form the place that it is created, but in the end I almost always move it to a separate scope so that I can strictly control what the eval sees. So I understand technically what you and matz are saying, but from a practical point a view I think #binding would be most useful if it was defined as something akin to: def binding(locals={}) locals.each{|k,v| local_varaible_set(k,v) } return binding end Such that if binding is called without an explicit receiver, it behaves at it does now, but with one as above. As it stands no one every uses binding with a receiver b/c it's meaningless.