Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add Object#instance_exec and Proc#bind
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3469 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
- Loading branch information
1 parent
9c24899
commit d921b79
Showing
5 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require 'test/unit' | ||
require File.dirname(__FILE__) + '/../../lib/active_support/core_ext/proc' | ||
|
||
class ProcTests < Test::Unit::TestCase | ||
def test_bind_returns_method_with_changed_self | ||
block = Proc.new { self } | ||
assert_equal self, block.call | ||
bound_block = block.bind("hello") | ||
assert_not_equal block, bound_block | ||
assert_equal "hello", bound_block.call | ||
end | ||
end |