Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   unbinding a Proc? (http://www.velocityreviews.com/forums/t822210-unbinding-a-proc.html)

Eric Mahurin 06-05-2005 01:56 PM

unbinding a Proc?
 
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




ES 06-05-2005 02:14 PM

Re: unbinding a Proc?
 

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



Yukihiro Matsumoto 06-05-2005 02:19 PM

Re: unbinding a Proc?
 
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 06-05-2005 02:46 PM

Re: unbinding a Proc?
 
--- 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.yahoo.com/



ES 06-05-2005 03:30 PM

Re: unbinding a Proc?
 

Le 5/6/2005, "Yukihiro Matsumoto" <matz@ruby-lang.org> a =E9crit:
>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 =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(); }



Logan Capaldo 06-05-2005 06:33 PM

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




All times are GMT. The time now is 04:45 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.