In message "[ruby-talk:01009] Reparenting a block"
on 99/12/31, Dave Thomas <Dave / thomases.com> writes:
|Another block question. Is there any way to re-parent a Proc?
An Block has the bindings at the point where it appear, including
self, local variables, constant bindings, etc. You can alternate self
in the block bindings by using instance_eval(), as Masaki stated in
[ruby-talk:01010]. Other than self, you can alter constant binding by
using class_eval()/module_eval().
def foo(&block)
Math.module_eval(&block)
end
foo {print PI} # 3.141592654
print PI # NameError!
self and constant bindings are only modifiable bindings in the block.
You cannot alter other bindings.
Ruby/Tk is using this technics.
TkButton::new(parent) {
label "hello world"
command { exit }
}
invokes methods of a newly created TkButton object. But it may be the
cause of confusion, e.g.
Dave::new {
@answer = 42 # modify Dave's instance variable.
}
print @answer # @answer is not set here.
Hope this helps you in new millennium. :-)
Happy new year.
matz.