From d921b799747aacb47dc8bbca9b2eefe25159e9d5 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Mon, 23 Jan 2006 06:40:43 +0000 Subject: Add Object#instance_exec and Proc#bind git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3469 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/active_support/core_ext/object_and_class.rb | 6 ++++++ activesupport/lib/active_support/core_ext/proc.rb | 12 ++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/proc.rb (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/object_and_class.rb b/activesupport/lib/active_support/core_ext/object_and_class.rb index 4856a9f5d5..d44afbd839 100644 --- a/activesupport/lib/active_support/core_ext/object_and_class.rb +++ b/activesupport/lib/active_support/core_ext/object_and_class.rb @@ -65,6 +65,12 @@ class Object #:nodoc: def to_json ActiveSupport::JSON.encode(self) end + + unless defined? instance_exec # 1.9 + def instance_exec(*arguments, &block) + block.bind(self)[*arguments] + end + end end class Class #:nodoc: diff --git a/activesupport/lib/active_support/core_ext/proc.rb b/activesupport/lib/active_support/core_ext/proc.rb new file mode 100644 index 0000000000..2ca23f62ef --- /dev/null +++ b/activesupport/lib/active_support/core_ext/proc.rb @@ -0,0 +1,12 @@ +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 -- cgit v1.2.3