From: "David A. Black" Date: 2005-10-03T02:58:55+09:00 Subject: Re: Eval and block problems --8323328-2093155883-1128275932=:3645 Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-2093155883-1128275932=:3645" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323328-2093155883-1128275932=:3645 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Mon, 3 Oct 2005, [ISO-8859-1] Brian Schr=F6der wrote: > Please help me to wrap my mind around this. I want to achieve the followi= ng: > > class A > def self.def_me(cmd, &block) > eval %( > def #{cmd}() > do things > # CALL BLOCK > end > ) > end > > def_me("foo") do > # access instance of A > end > end > > A.new.foo > > and have the block from the dynamically created method foo be > evaluated in the binding of the instance of A. > > I'm sure this is possible, but I've never really grocked eval, > instance_eval and class_eval. I doubt this is the final word in it but see if this helps: class A def self.def_me(cmd, &block) define_method(cmd) do puts "Defining method" block.call(self) end end def_me("foo") do |a| puts "Here in block" puts "self is #{self}, a is #{a}" end end A.new.foo David --=20 David A. Black dblack@wobblini.net --8323328-2093155883-1128275932=:3645-- --8323328-2093155883-1128275932=:3645--