-
-
Save joevandyk/788826 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class C | |
# I want to define methods on this | |
# particular C object that access | |
# stuff from b. | |
def self.from_b b | |
self.new.tap do |c| | |
eval "def c.foo; #{ b.foo }; end " | |
eval "def c.gob; #{ b.foo }; end " | |
end | |
end | |
end | |
class B | |
attr_accessor :foo | |
def c | |
C.from_b(self) | |
end | |
end | |
require 'test/unit' | |
class T < Test::Unit::TestCase | |
def test_this | |
b = B.new | |
b.foo = 2 | |
assert_equal b.foo, b.c.foo | |
assert_equal b.foo, b.c.gob | |
b.foo = 3 | |
assert_equal b.foo, b.c.foo | |
assert_equal b.foo, b.c.gob | |
assert_raises(NoMethodError) { C.new.foo } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment