Ruby Forum Ruby > More rebinding blocks

Posted by Mat Schaffer (Guest)
on 16.06.2006 18:48
(Received via mailing list)
Sorry to beat a dead horse, here.  But after reading through the
older discussions on google groups, I'm still left with a question.

Arbitrarily rebinding blocks seems like it would be amazingly
useful.  Is there some fundamental reason that the following code is
impossible?  Or has it just not been implemented yet.

class MyClass
   def runhere(b)
     @a = 3
     b.binding = self.binding
     b.call
   end
end

MyClass.new.runhere(proc { puts @a })

I know instance_eval can do this simple case, but as covered in
previous conversations (http://tinyurl.com/ehq8x [google ruby-talk]),
you loose the ability pass arguments to the block.  Matz proposed an
UnboundMethod workaround.  Dave proposed an extension of
instance_eval.  But why not extend Proc to accept new bindings instead?

Is there something I don't know that maybe I should?

-Mat
Posted by Logan Capaldo (Guest)
on 16.06.2006 18:58
(Received via mailing list)
On Jun 16, 2006, at 12:46 PM, Mat Schaffer wrote:

>     b.binding = self.binding
> of instance_eval.  But why not extend Proc to accept new bindings  
> instead?
>
> Is there something I don't know that maybe I should?
>
> -Mat
>
>

1.9 has #instance_exec which is #instance_eval + the ability to pass
args. (The urge for me to actually check out 1.9 grows daily.)
Posted by Mat Schaffer (Guest)
on 16.06.2006 20:29
(Received via mailing list)
On Jun 16, 2006, at 12:56 PM, Logan Capaldo wrote:
>>   def runhere(b)
>>     @a = 3
>>     b.binding = self.binding
>>     b.call
>>   end
>> end
>>
>> MyClass.new.runhere(proc { puts @a })
>
> 1.9 has #instance_exec which is #instance_eval + the ability to  
> pass args. (The urge for me to actually check out 1.9 grows daily.)

Oh nice.  That would work.  It doesn't exactly answer my question,
though.  But I guess the answer could just be that the community
didn't decide on that particular syntax.

Can Matz or anyone offer any assurance that that feature of 1.9 will
survive in later releases?  I'd hate to write code that uses it only
to loose it in a future release.

Thanks,
Mat
Posted by Sean O'halpin (sean)
on 16.06.2006 21:15
(Received via mailing list)
On 6/16/06, Mat Schaffer <schapht@gmail.com> wrote:
> Can Matz or anyone offer any assurance that that feature of 1.9 will
> survive in later releases?  I'd hate to write code that uses it only
> to loose it in a future release.

I think you can be pretty sure it will survive. You can also find an
implementation by Ara Howard for 1.8 in ruby-talk:156726.

Regards,
Sean
Posted by Mauricio Fernandez (Guest)
on 16.06.2006 21:46
(Received via mailing list)
On Sat, Jun 17, 2006 at 04:14:06AM +0900, Sean O'Halpin wrote:
> On 6/16/06, Mat Schaffer <schapht@gmail.com> wrote:
> >Can Matz or anyone offer any assurance that that feature of 1.9 will
> >survive in later releases?  I'd hate to write code that uses it only
> >to loose it in a future release.
> 
> I think you can be pretty sure it will survive. You can also find an
> implementation by Ara Howard for 1.8 in ruby-talk:156726.

There are many implementations around. In addition to the above one, Ara
posted another on [[ruby-core:6380]. Both are thread-safe, but will not 
work
in general with frozen objects. They're still better than the one you 
can find
in ActiveSupport, which is not strictly thread-safe (it is in practice,
though).
I wrote yet another implementation which is thread-safe _and_ works with
frozen objects; you can find it at
    http://eigenclass.org/hiki.rb?instance_exec
where I also compare it to some other ones.
Posted by Mat Schaffer (Guest)
on 16.06.2006 21:56
(Received via mailing list)
On Jun 16, 2006, at 3:43 PM, Mauricio Fernandez wrote:
> one, Ara
>     http://eigenclass.org/hiki.rb?instance_exec
> where I also compare it to some other ones.

You, my friend, possess an impressive intellect.  Thanks so much.
-Mat