unbinding a Proc?

Discussion in 'Ruby' started by Eric Mahurin, Jun 5, 2005.

  1. Eric Mahurin

    Eric Mahurin Guest

    This is mostly a curiosity thing...

    Is there a way to unbind a Proc from the binding it was created
    in? What I mean by this is taking a Proc and looking
    up/calling everything it refers to outside of itself and
    replacing them with what was found - objects. Assignments to
    the outside and method calls that modify things on the outside
    should be considered illegal.

    The application for doing this would be to get rid of many
    *eval(string) calls which I think are evil because you are
    parsing/recompiling code at run-time - a severe limitation to
    optimization. I think many of the times that you use
    *eval(string) instead of *eval(proc) (or Proc#call), you do so
    because you take the values of certain variables and put it in
    the string (with #{...}). You want these to be constant
    instead of refer to these variables in your context all the
    time. This effectively "unbinds" those variables from their
    context.

    Another useful thing might be rebinding a Proc to a different
    Binding. But, I can't think of any applications off-hand.




    __________________________________
    Discover Yahoo!
    Find restaurants, movies, travel and more fun for the weekend. Check it out!
    http://discover.yahoo.com/weekend.html
     
    Eric Mahurin, Jun 5, 2005
    #1
    1. Advertising

  2. Eric Mahurin

    ES Guest

    Le 5/6/2005, "Eric Mahurin" <> a =E9crit:
    >This is mostly a curiosity thing...
    >
    >Is there a way to unbind a Proc from the binding it was created
    >in? What I mean by this is taking a Proc and looking
    >up/calling everything it refers to outside of itself and
    >replacing them with what was found - objects. Assignments to
    >the outside and method calls that modify things on the outside
    >should be considered illegal.
    >
    >The application for doing this would be to get rid of many
    >*eval(string) calls which I think are evil because you are
    >parsing/recompiling code at run-time - a severe limitation to
    >optimization. I think many of the times that you use
    >*eval(string) instead of *eval(proc) (or Proc#call), you do so
    >because you take the values of certain variables and put it in
    >the string (with #{...}). You want these to be constant
    >instead of refer to these variables in your context all the
    >time. This effectively "unbinds" those variables from their
    >context.
    >
    >Another useful thing might be rebinding a Proc to a different
    >Binding. But, I can't think of any applications off-hand.


    Proc#rebind would be very useful but I am still waiting for
    a full-blown first-order method conversion, too.

    E

    --
    template<typename duck>
    void quack(duck& d) { d.quack(); }
     
    ES, Jun 5, 2005
    #2
    1. Advertising

  3. Hi,

    In message "Re: unbinding a Proc?"
    on Sun, 5 Jun 2005 23:14:58 +0900, ES <> writes:

    |Proc#rebind would be very useful but I am still waiting for
    |a full-blown first-order method conversion, too.

    Can you explain what you want? I am not sure how Proc#rebind would
    work, nor "a full-blown first-order method conversion".

    matz.
     
    Yukihiro Matsumoto, Jun 5, 2005
    #3
  4. Eric Mahurin

    Eric Mahurin Guest

    --- Yukihiro Matsumoto <> wrote:
    > In message "Re: unbinding a Proc?"
    > on Sun, 5 Jun 2005 23:14:58 +0900, ES
    > <> writes:
    >
    > |Proc#rebind would be very useful but I am still waiting for
    > |a full-blown first-order method conversion, too.
    >
    > Can you explain what you want? I am not sure how Proc#rebind
    > would
    > work, nor "a full-blown first-order method conversion".
    >
    > matz.


    If ES is thinking like me it would be called like this:

    Proc#rebind(anotherBinding)

    It would rebind all variable references and method calls into
    the original binding to anotherBinding.

    I think this would be more useful though:

    Proc#unbind

    It would replace all variable references and method calls into
    the original binding with the current objects they return.
    This would be great for changing most *eval(aString) to
    *eval(aProc.unbind). I assume *eval(aProc) is much better than
    *eval(aString) especially from a ruby optimization standpoint
    (and ruby2c).






    __________________________________
    Discover Yahoo!
    Use Yahoo! to plan a weekend, have fun online and more. Check it out!
    http://discover.yahoo.com/
     
    Eric Mahurin, Jun 5, 2005
    #4
  5. Eric Mahurin

    ES Guest

    Le 5/6/2005, "Yukihiro Matsumoto" <> a =E9crit:
    >Hi,
    >
    >In message "Re: unbinding a Proc?"
    > on Sun, 5 Jun 2005 23:14:58 +0900, ES <> writes:
    >
    >|Proc#rebind would be very useful but I am still waiting for
    >|a full-blown first-order method conversion, too.
    >
    >Can you explain what you want? I am not sure how Proc#rebind would
    >work, nor "a full-blown first-order method conversion".


    Rebinding: normal binding works this way (as you probably know:):

    class A
    attr_accessor :a
    def foo b
    @a =3D 'in A'
    b.bar {puts self.a}
    end
    end

    class B
    attr_accessor :a
    def bar(&p)
    @a =3D 'in B'
    p.call
    end
    end

    a =3D A.new
    b =3D B.new
    a.foo b # =3D> 'in A'

    Same applies for all other circumstances, as well, the block is
    bound at the time of its creation. While this is typically quite
    satisfactory, on occasion it would be useful to rebind the block
    at another location -essentially, it would be the same as (from
    our example) eval()ing the {puts self.a} again wherever the #rebind
    is called.

    "Full-blown first-order method conversion": conversion referring
    to change from the current way of working methods.. this idea is
    perhaps best described by a hypothetical syntactic sugarization:
    currently, def methodname is a primitive (not a method call itself),
    but it could actually be a method that takes a block (the method
    body) and binds it at the call location (i.e. to the class it is
    defined in).

    At the basic level, everything would be based on blocks, methods
    just being a specific use case (probably with some convenience
    added). A method dictionary might be a good way to think about
    it for methods.

    I have dabbled in trying to implement this (and it should not be
    too hard for more experienced developers, although it would be
    a shift in paradigm); the main problem I have run to is to figure
    out what, instead of 'def', would be the primitive operation. That
    and lacking skill :)

    The disadvantage I see in this type of method handling is that
    it may interfere with the conceptual model if you consider that
    methods (or 'behaviours') should be intrinsic and inseparable to
    and from objects.

    >=09=09=09=09=09=09=09matz.


    E

    --
    template<typename duck>
    void quack(duck& d) { d.quack(); }
     
    ES, Jun 5, 2005
    #5
  6. On 6/5/05, Yukihiro Matsumoto <> wrote:
    > Hi,
    >=20
    > In message "Re: unbinding a Proc?"
    > on Sun, 5 Jun 2005 23:14:58 +0900, ES <> write=

    s:
    >=20
    > |Proc#rebind would be very useful but I am still waiting for
    > |a full-blown first-order method conversion, too.
    >=20
    > Can you explain what you want? I am not sure how Proc#rebind would
    > work, nor "a full-blown first-order method conversion".
    >=20
    > matz.
    >=20
    >=20


    I think what he wants is (optionally) dynamically scoped closures.
     
    Logan Capaldo, Jun 5, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi
    Replies:
    3
    Views:
    1,996
    David Lozzi
    Jun 1, 2005
  2. Sin

    Re: UNbinding a socket

    Sin, Jun 23, 2003, in forum: C++
    Replies:
    2
    Views:
    13,917
  3. NevilleDNZ
    Replies:
    9
    Views:
    463
    NevilleDNZ
    Aug 16, 2006
  4. Jean-Hugues ROBERT

    Why no Proc##[]=() ? Why no Proc##replace() ?

    Jean-Hugues ROBERT, May 1, 2004, in forum: Ruby
    Replies:
    14
    Views:
    316
    Jean-Hugues ROBERT
    May 5, 2004
  5. Minkoo Seo

    Proc vs lambda vs proc

    Minkoo Seo, Feb 4, 2007, in forum: Ruby
    Replies:
    19
    Views:
    262
    Brian Candler
    Feb 6, 2007
Loading...

Share This Page