Skip to content

Instantly share code, notes, and snippets.

@JackDanger
Forked from joevandyk/t.rb
Created January 21, 2011 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JackDanger/788999 to your computer and use it in GitHub Desktop.
Save JackDanger/788999 to your computer and use it in GitHub Desktop.
class C
attr_accessor :b
def foo
b ? b.foo : raise(NoMethodError.new)
end
alias gob foo
def self.from_b b
new.tap {|c| c.instance_variable_set '@b', b }
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