tenderlove (owner)

Revisions

gist: 193252 Download_button fork
public
Public Clone URL: git://gist.github.com/193252.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'test/unit'
 
module InheritIV
  def inherit_iv *args
    @ih ||= []
    @ih += args
    args.each do |iv|
      self.class_eval("class << self; attr_accessor :#{iv}; end")
    end
  end
 
  def inherited klass
    @ih.each do |iv|
      klass.send(:"#{iv}=", send(iv))
    end
    klass.instance_variable_set(:@ih, @ih)
  end
end
 
class First
  extend InheritIV
  inherit_iv :foo
 
  @foo = 1
 
  def foo
    self.class.foo
  end
 
  def bar
    self.class.bar
  end
end
 
class Second < First
  inherit_iv :bar
 
  @foo = 2
  @bar = 3
end
 
class Third < Second
end
 
class Testy < Test::Unit::TestCase
  def setup
    @first = First.new
    @second = Second.new
    @third = Third.new
  end
 
  def test_first
    assert_equal 1, @first.foo
  end
 
  def test_second
    assert_equal 2, @second.foo
    assert_equal 3, @second.bar
  end
 
  def test_third
    assert_equal 2, @third.foo
    assert_equal 3, @third.bar
  end
end