From: Robert Klemme Date: 2009-10-09T05:40:12+09:00 Subject: Re: Mixing instance_eval and block with arguments On 10/08/2009 06:52 PM, Jesús Gabriel y Galán wrote: > On Thu, Oct 8, 2009 at 6:04 PM, Robert Klemme > wrote: >> Hi, >> >> here's what I'd like to do in a silly example: >> >> S = Struct.new :foo, :bar >> >> init = lambda do |a, b| >> self.foo = b >> self.bar = a + 10 >> end >> >> s = S.new # or create an instance otherwise >> >> # now comes the fun part which does not work >> s.instance_eval(1, 2, &init) >> => # >> >> In other words: I like to define a block as an initializer which I can >> store away somewhere and invoke that initializer later on in the >> context of "self" and pass arguments at the same time. Any ideas how >> I can accomplish this elegantly? > > Probably not exactly what you want (can't make self to not be main > inside the lambda), but what about: > > irb(main):001:0> S = Struct.new :foo, :bar > => S > irb(main):003:0> s = S.new > => # > irb(main):040:0> def s.init(*args,&blk) #you can put this in a module > and extend s with it, or add it to S > irb(main):041:1> blk[self,*args] > irb(main):042:1> end > irb(main):043:0> init = lambda do |o,a,b| > irb(main):044:1* o.foo = b > irb(main):045:1> o.bar = a + 10 > irb(main):046:1> end > => # > irb(main):047:0> s.init(1,2,&init) > => 11 > irb(main):048:0> s > => # > > Don't know if this is good enough. Thank you! Unfortunately I tried to avoid explicit passing of the reference. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/