From: ara.t.howard@... Date: 2006-07-09T15:09:52+09:00 Subject: Re: instance_exec On Sat, 8 Jul 2006 transfire@gmail.com wrote: > Mauricio Fernandez's looks better though --think I'll have to rip it > for Facets ;-) i just came across a bug in it (it's not re-entrant) - here's the fix: harp:~ > cat a.rb class Object unless instance_methods.include? 'instance_exec' def instance_exec *a, &b m, n = nil, -1 loop{ m = "__instance_exec_#{ Thread.current.object_id.abs }_#{ n += 1 }__" break unless respond_to? m } singleton_class{ define_method m, &b } send m, *a ensure singleton_class{ undef_method m } end end unless instance_methods.include? 'singleton_class' def singleton_class &b sc = class << self; self; end sc.module_eval &b if b sc end end end a = [] a.instance_exec 42 do |x| instance_exec x do |y| push y p size end end harp:~ > ruby a.rb 1 figured you'd use this if anyone would tom... cheers. -a -- suffering increases your inner strength. also, the wishing for suffering makes the suffering disappear. - h.h. the 14th dali lama