On Thu, 26 Apr 2007 12:21:56 -0500, "Nick Sieger" <[EMAIL PROTECTED]> wrote:
> I'd have no problems here -- but how to prevent application code from
> overriding a good implementation with a faulty one -- are people
> starting to write code that conditionally defines it?
Rails does -- from active_support/core_ext/object/extending.rb:
unless defined? instance_exec # 1.9
def instance_exec(*arguments, &block)
block.bind(self)[*arguments]
end
end
I think most of the use of instance_exec in the wild is in Rails code actually.
Their implementation of it (factored out into Proc#bind) is better than most,
but still leaves something to be desired performance-wise:
class Proc #:nodoc:
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
(It also has problems with thread safety -- the correct approach would have
been to use the current thread's object_id, rather than a timestamp.)
So, yeah, if we can provide our own instance_exec, Rails apps should benefit.
-mental
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email