On Sat, 8 Jul 2006, Marcel Molina Jr. wrote: > On Sat, Jul 08, 2006 at 02:22:31AM +0900, ara.t.howard / noaa.gov wrote: >> >> i seems to recall someone came up with an impl of instance_exec >> (instance_eval >> that takes args) a while back - might have even been me! ;-) anyone >> remember? > > There is one in Rails' ActiveSupport: > > class Object > unless defined? instance_exec # 1.9 > def instance_exec(*arguments, &block) > block.bind(self)[*arguments] > end > end > end > > class Proc > def bind(object) > block, time = self, Time.now > (class << object; self end).class_eval do > method_name = "__bind_#{time.to_i}_#{time.usec}" > define_method(method_name, &block) > method = instance_method(method_name) > remove_method(method_name) > method > end.bind(object) > end > end thanks! i just hacked this one out: harp:~ > cat a.rb class Object unless instance_methods.include? 'instance_exec' def instance_exec *a, &b m = "__instance_exec_#{ Thread.current.object_id.abs }__" singleton_class{ define_method m, &b } send m, *a ensure singleton_class{ undef_method m } rescue nil 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 |elem| push elem end p a harp:~ > ruby a.rb [42] regards. -a -- suffering increases your inner strength. also, the wishing for suffering makes the suffering disappear. - h.h. the 14th dali lama