Hi,
In message "[ruby-talk:4887] Re: Re-binding a block"
on 00/09/12, Dave Thomas <Dave / thomases.com> writes:
|Now, say I wanted to make it more complicated and pass parameters in
|to the block. Would it be reasonable to change instance_eval so that
|if block_given? was true, any parameters given to instance_eval were
|passed to the block? Or am I missing (yet another) clever trick?
I don't think it's good idea to change instance_eval. How about this
yet another trick (using yet undocumented unbound method).
class Matz2
def internal(a)
puts @name
end
@@block = self.instance_method(:internal)
@name = "class level"
def initialize
@name = "instance level"
end
def block
@@block.bind(self).call(42)
end
end
Matz2.new.block
I'm amazing myself how many tricks Ruby provide. It's far beyond the
author expected.
matz.