From: Sean O'Halpin Date: 2006-07-08T02:47:19+09:00 Subject: Re: instance_exec On 7/7/06, 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 > > marcel > -- > Marcel Molina Jr. > > This is not thread-safe - relying on time.usec may not work on sufficiently fast or clock-impaired machines. Regards, Sean