6章で実施したこと
- Franc、Dollarの親クラスMoneyの追加
- Franc、Dollarの*()メソッドをMoneyに移動
money.rb
#!/usr/bin/ruby
class Money
attr_accessor :amount
protected :amount
def ==(other)
return @amount == other.amount
end
end
class Dollar < Money
def initialize(amount)
@amount = amount
end
def *(multiplier)
return Dollar.new(@amount * multiplier)
end
end
class Franc < Money
def initialize(amount)
@amount = amount
end
def *(multiplier)
return Franc.new(@amount * multiplier)
end
end
money_test.rb
#!/usr/bin/ruby
require 'test/unit'
require 'money'
class TestMoney < Test::Unit::TestCase
def test_multiplication
five = Dollar.new(5)
assert_equal(Dollar.new(10), five * 2)
assert_equal(Dollar.new(15), five * 3)
end
def test_equality
assert(Dollar.new(5) == Dollar.new(5))
assert(Dollar.new(5) != Dollar.new(6))
assert(Franc.new(5) == Franc.new(5))
assert(Franc.new(5) != Franc.new(6))
end
def test_Franc_multiplication
five = Franc.new(5)
assert_equal(Franc.new(10), five * 2)
assert_equal(Franc.new(15), five * 3)
end
end