From: Eero Saynatkari Date: 2006-08-28T01:18:03+09:00 Subject: Re: Nuby with instance_eval question Geoff Barnes wrote: > Wondering why this works the way it does: > > 1 class Klass > 2 > 3 attr_accessor :attr1 > 4 > 5 def initialize (&block) > 6 chum = "chum_in_Klass" > 7 instance_eval(&block) # Why does instance_eval pick up > self.attr1 from Klass but not 'chum'?? > 8 eval_attr1 > 9 end > 10 def eval_attr1 > 11 @ok = self.attr1.call > 12 end > 13 end > 14 > 15 chum = "chum_in_main" > 16 > 17 foo=Klass.new { > 18 self.attr1 = proc { chum } > 19 } You are creating a closure here and, since 'chum' exists in this scope (you would get an error otherwise), it is bound here. > 20 > 21 puts foo.inspect # -> foo.ok = "chum_in_main" > 22 > 23 chum="chum_in_main_again" > 24 foo.eval_attr1 > 25 > 26 puts foo.inspect # -> foo.ok = "chum_in_main_again" > > > > Thanks -- Posted via http://www.ruby-forum.com/.