From: "Ara.T.Howard" Date: 2005-09-20T00:34:48+09:00 Subject: Re: help with tricky proc/binding issue On Mon, 19 Sep 2005, Yukihiro Matsumoto wrote: > Hi, > > In message "Re: help with tricky proc/binding issue" > on Mon, 19 Sep 2005 23:42:12 +0900, "Ara.T.Howard" writes: > > |i'm trying to be able to define a proc that can be called in the context of > |self, eg: > | > | block = lambda{ p 42 } > | an_obj.instance_eval &block > | > |but which can also be called with arguments, for example (imaginary ruby): > > I understand what you want. But I haven't come up with a good name > for the function yet. maybe class Object def evaluate(*args, &block) ... end end ?? another option would be to turn the problem inside out and do something like l = lambda{ p x } l.self = obj l.call this is what i have so far: [ahoward@localhost ~]$ cat a.rb class Object def evaluate(*a, &b) m = "____eval____#{ rand(65536) }____" klass = Class === self ? self : self::class klass.module_eval{ define_method m, &b } begin send m, *a ensure klass.module_eval{ remove_method m } end end end class C def y 42 end end b = lambda{|x| p [x, y, self]} c = C::new x = 42 c.evaluate(x, &b) #=> [42, 42, #] [ahoward@localhost ~]$ ruby a.rb [42, 42, #] thoughts? -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | Your life dwells amoung the causes of death | Like a lamp standing in a strong breeze. --Nagarjuna ===============================================================================