Skip to content

Instantly share code, notes, and snippets.

@joevandyk
Created January 20, 2011 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joevandyk/788826 to your computer and use it in GitHub Desktop.
Save joevandyk/788826 to your computer and use it in GitHub Desktop.
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