[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

unbinding a Proc?

Eric Mahurin

6/5/2005 1:56:00 PM

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/we...



5 Answers

ES

6/5/2005 2:15:00 PM

0


Le 5/6/2005, "Eric Mahurin" <eric_mahurin@yahoo.com> a écrit:
>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(); }


Yukihiro Matsumoto

6/5/2005 2:19:00 PM

0

Hi,

In message "Re: unbinding a Proc?"
on Sun, 5 Jun 2005 23:14:58 +0900, ES <ruby-ml@magical-cat.org> 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.


Eric Mahurin

6/5/2005 2:47:00 PM

0

--- Yukihiro Matsumoto <matz@ruby-lang.org> wrote:
> In message "Re: unbinding a Proc?"
> on Sun, 5 Jun 2005 23:14:58 +0900, ES
> <ruby-ml@magical-cat.org> 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....


ES

6/5/2005 3:30:00 PM

0


Le 5/6/2005, "Yukihiro Matsumoto" <matz@ruby-lang.org> a écrit:
>Hi,
>
>In message "Re: unbinding a Proc?"
> on Sun, 5 Jun 2005 23:14:58 +0900, ES <ruby-ml@magical-cat.org> 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 = 'in A'
b.bar {puts self.a}
end
end

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

a = A.new
b = B.new
a.foo b # => '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.

> matz.

E

--
template<typename duck>
void quack(duck& d) { d.quack(); }


Logan Capaldo

6/5/2005 6:34:00 PM

0

On 6/5/05, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:
> Hi,
>
> In message "Re: unbinding a Proc?"
> on Sun, 5 Jun 2005 23:14:58 +0900, ES <ruby-ml@magical-cat.org> 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.
>
>

I think what he wants is (optionally) dynamically scoped closures.