aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-09-24 23:00:42 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-09-24 23:00:42 +0000
commit8e78a4542e80a2d8d593547ecf8021b0e2902a0d (patch)
tree2a67622da0f95e8465c95c29b5cdb22d91e467eb /activesupport/lib/active_support
parenteca3b790b5aaa92cc563dfed095feffda150c09e (diff)
downloadrails-8e78a4542e80a2d8d593547ecf8021b0e2902a0d.tar.gz
rails-8e78a4542e80a2d8d593547ecf8021b0e2902a0d.tar.bz2
rails-8e78a4542e80a2d8d593547ecf8021b0e2902a0d.zip
Object#instance_exec produces fewer garbage methods.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7621 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r--activesupport/lib/active_support/core_ext/object/extending.rb27
1 files changed, 24 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/extending.rb b/activesupport/lib/active_support/core_ext/object/extending.rb
index 7c79d73f21..19f42cabce 100644
--- a/activesupport/lib/active_support/core_ext/object/extending.rb
+++ b/activesupport/lib/active_support/core_ext/object/extending.rb
@@ -42,10 +42,31 @@ class Object
values
end
end
-
+
unless defined? instance_exec # 1.9
- def instance_exec(*arguments, &block) #:nodoc:
- block.bind(self)[*arguments]
+ module InstanceExecMethods #:nodoc:
+ end
+ include InstanceExecMethods
+
+ # Evaluate the block with the given arguments within the context of
+ # this object, so self is set to the method receiver.
+ #
+ # From Mauricio's http://eigenclass.org/hiki/bounded+space+instance_exec
+ def instance_exec(*args, &block)
+ begin
+ old_critical, Thread.critical = Thread.critical, true
+ n = 0
+ n += 1 while respond_to?(method_name = "__instance_exec#{n}")
+ InstanceExecMethods.module_eval { define_method(method_name, &block) }
+ ensure
+ Thread.critical = old_critical
+ end
+
+ begin
+ send(method_name, *args)
+ ensure
+ InstanceExecMethods.module_eval { remove_method(method_name) } rescue nil
+ end
end
end
end