Решение на Шеста задача от Ангел Николов

Обратно към всички решения

Към профила на Ангел Николов

Резултати

  • 1 точка от тестове
  • 0 бонус точки
  • 1 точка общо
  • 7 успешни тест(а)
  • 40 неуспешни тест(а)

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
def initialize
@rates = {}
end
def set(from_currency, to_currency, rate)
@rates[[from_currency, to_currency]] = rate
end
def get(from_currency, to_currency)
@rates[[from_currency, to_currency]] or
1 / @rates[[to_currency, from_currency]]
end
end
class Money
def initialize(amount, currency)
@amount = amount
@currency = currency
end
attr_reader :amount, :currency
def to_s
"#{@amount.truncate(2).to_s} #{@currency.to_s}"
end
def in(currency, exchange_rate)
rate = exchange_rate.get(@currency, currency)
if rate
currency * rate
else
nil
end
end
end

Лог от изпълнението

...FF..FFFFFF..FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

Failures:

  1) ExchangeRate#set sets the exchange rate in both directions
     Failure/Error: rate.get(:EUR, :BGN).should eq '1.25'.to_d
       
       expected: #<BigDecimal:9175d28,'0.125E1',18(18)>
            got: #<BigDecimal:93a687c,'0.2E1',9(36)>
       
       (compared using ==)
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:25:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) ExchangeRate#get returns nil for non-existing rates
     Failure/Error: rate.get(:EUR, :BGN).should be_nil
     TypeError:
       nil can't be coerced into Fixnum
     # /tmp/d20130203-23049-1h1u3n2/solution.rb:15:in `/'
     # /tmp/d20130203-23049-1h1u3n2/solution.rb:15:in `get'
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:31:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) ExchangeRate#get always returns 1 as the exchange rate between two identical currencies
     Failure/Error: rate.get(:JPY, :JPY).should eq 1.to_d
     TypeError:
       nil can't be coerced into Fixnum
     # /tmp/d20130203-23049-1h1u3n2/solution.rb:15:in `/'
     # /tmp/d20130203-23049-1h1u3n2/solution.rb:15:in `get'
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:45:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) ExchangeRate#get does not allow changing the exchange rate between two identical currencies
     Failure/Error: rate.get(:EUR, :EUR).should eq 1.to_d
       
       expected: #<BigDecimal:9181fd8,'0.1E1',9(36)>
            got: #<BigDecimal:9182028,'0.2E1',9(36)>
       
       (compared using ==)
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:50:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) ExchangeRate#convert raises an ExchangeRate::Unknown exception when the rate is not defined
     Failure/Error: end.to raise_error(ExchangeRate::Unknown)
     NameError:
       uninitialized constant ExchangeRate::Unknown
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:58:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) ExchangeRate#convert converts from A to B using an existing rate A -> B
     Failure/Error: rate.convert(:EUR, :BGN, 100.to_d).should eq '195.583'.to_d
     NoMethodError:
       undefined method `convert' for #<ExchangeRate:0x918e1e8>
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:63:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) ExchangeRate#convert converts from B to A using an existing rate A -> B
     Failure/Error: rate.convert(:BGN, :EUR, 100.to_d).should eq 50.to_d
     NoMethodError:
       undefined method `convert' for #<ExchangeRate:0x918d360>
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:68:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  8) ExchangeRate#convert works for identical currencies without defining any rates
     Failure/Error: rate.convert(:JPY, :JPY, 123.to_d).should eq 123.to_d
     NoMethodError:
       undefined method `convert' for #<ExchangeRate:0x919bb04 @rates={}>
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:72:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  9) Money has a custom to_s representation
     Failure/Error: Money.new('12.99'.to_d, :USD).to_s.should eq '12.99 USD'
       
       expected: "12.99 USD"
            got: "0.1299E2 USD"
       
       (compared using ==)
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:89:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  10) Money convertion allows convertion to other currencies
     Failure/Error: levas = euros.in(:BGN, rate)
     NoMethodError:
       undefined method `*' for :BGN:Symbol
     # /tmp/d20130203-23049-1h1u3n2/solution.rb:34:in `in'
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:100:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  11) Money convertion does not change the amount if the same currency is passed
     Failure/Error: Money.new(5.to_d, :EUR).in(:EUR, ExchangeRate.new).amount.should eq 5.to_d
     TypeError:
       nil can't be coerced into Fixnum
     # /tmp/d20130203-23049-1h1u3n2/solution.rb:15:in `/'
     # /tmp/d20130203-23049-1h1u3n2/solution.rb:15:in `get'
     # /tmp/d20130203-23049-1h1u3n2/solution.rb:32:in `in'
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:107:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  12) Money convertion raises an ExchangeRate::Unknown exception for unknown rates
     Failure/Error: end.to raise_error(ExchangeRate::Unknown)
     NameError:
       uninitialized constant ExchangeRate::Unknown
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:113:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  13) Money arithmetic allows * with a numeric
     Failure/Error: result = bucks.public_send(operation, numeric)
     NoMethodError:
       undefined method `*' for 0.5E1 USD:Money
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:123:in `public_send'
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:123:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  14) Money arithmetic * with money objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `*' for 0.1E2 USD:Money>
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:129:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  15) Money arithmetic allows / with a numeric
     Failure/Error: result = bucks.public_send(operation, numeric)
     NoMethodError:
       undefined method `/' for 0.5E1 USD:Money
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:123:in `public_send'
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:123:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  16) Money arithmetic / with money objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `/' for 0.1E2 USD:Money>
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:129:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  17) Money arithmetic + with numeric objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `+' for 0.1E2 USD:Money>
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:137:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  18) Money arithmetic allows + with money with the same currency
     Failure/Error: result = a.public_send(operation, b)
     NoMethodError:
       undefined method `+' for 0.15E2 EUR:Money
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:146:in `public_send'
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:146:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  19) Money arithmetic raises Money::IncompatibleCurrencies for + with money with different currencies
     Failure/Error: end.to raise_error(Money::IncompatibleCurrencies)
     NameError:
       uninitialized constant Money::IncompatibleCurrencies
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:154:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  20) Money arithmetic - with numeric objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `-' for 0.1E2 USD:Money>
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:137:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  21) Money arithmetic allows - with money with the same currency
     Failure/Error: result = a.public_send(operation, b)
     NoMethodError:
       undefined method `-' for 0.15E2 EUR:Money
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:146:in `public_send'
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:146:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  22) Money arithmetic raises Money::IncompatibleCurrencies for - with money with different currencies
     Failure/Error: end.to raise_error(Money::IncompatibleCurrencies)
     NameError:
       uninitialized constant Money::IncompatibleCurrencies
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:154:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  23) Money arithmetic + with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `+' for 0.1E2 USD:Money>
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:160:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  24) Money arithmetic - with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `-' for 0.1E2 USD:Money>
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:160:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  25) Money arithmetic * with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `*' for 0.1E2 USD:Money>
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:160:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  26) Money arithmetic / with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `/' for 0.1E2 USD:Money>
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:160:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  27) Money comparison works when currencies are the same
     Failure/Error: (a < b).should be_false
     NoMethodError:
       undefined method `<' for 0.1245E2 USD:Money
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:172:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  28) Money comparison works for equality when currencies are the same
     Failure/Error: (Money.new('12.45'.to_d, :BGN) == Money.new('12.45'.to_d, :BGN)).should be_true
       expected: true value
            got: false
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:189:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  29) Money comparison with <=> raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:195:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  30) Money comparison with <=> raises IncompatibleCurrencies when currencies differ
     Failure/Error: end.to raise_error(Money::IncompatibleCurrencies)
     NameError:
       uninitialized constant Money::IncompatibleCurrencies
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:203:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  31) Money comparison with == raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:195:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  32) Money comparison with == raises IncompatibleCurrencies when currencies differ
     Failure/Error: end.to raise_error(Money::IncompatibleCurrencies)
     NameError:
       uninitialized constant Money::IncompatibleCurrencies
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:203:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  33) Money comparison with < raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `<' for 0.12E2 USD:Money>
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:195:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  34) Money comparison with < raises IncompatibleCurrencies when currencies differ
     Failure/Error: end.to raise_error(Money::IncompatibleCurrencies)
     NameError:
       uninitialized constant Money::IncompatibleCurrencies
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:203:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  35) Money comparison with <= raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `<=' for 0.12E2 USD:Money>
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:195:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  36) Money comparison with <= raises IncompatibleCurrencies when currencies differ
     Failure/Error: end.to raise_error(Money::IncompatibleCurrencies)
     NameError:
       uninitialized constant Money::IncompatibleCurrencies
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:203:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  37) Money comparison with > raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `>' for 0.12E2 USD:Money>
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:195:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  38) Money comparison with > raises IncompatibleCurrencies when currencies differ
     Failure/Error: end.to raise_error(Money::IncompatibleCurrencies)
     NameError:
       uninitialized constant Money::IncompatibleCurrencies
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:203:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  39) Money comparison with >= raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `>=' for 0.12E2 USD:Money>
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:195:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  40) Money comparison with >= raises IncompatibleCurrencies when currencies differ
     Failure/Error: end.to raise_error(Money::IncompatibleCurrencies)
     NameError:
       uninitialized constant Money::IncompatibleCurrencies
     # /tmp/d20130203-23049-1h1u3n2/spec.rb:203:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.08436 seconds
47 examples, 40 failures

Failed examples:

rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:21 # ExchangeRate#set sets the exchange rate in both directions
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:30 # ExchangeRate#get returns nil for non-existing rates
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:44 # ExchangeRate#get always returns 1 as the exchange rate between two identical currencies
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:48 # ExchangeRate#get does not allow changing the exchange rate between two identical currencies
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:55 # ExchangeRate#convert raises an ExchangeRate::Unknown exception when the rate is not defined
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:61 # ExchangeRate#convert converts from A to B using an existing rate A -> B
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:66 # ExchangeRate#convert converts from B to A using an existing rate A -> B
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:71 # ExchangeRate#convert works for identical currencies without defining any rates
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:88 # Money has a custom to_s representation
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:95 # Money convertion allows convertion to other currencies
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:106 # Money convertion does not change the amount if the same currency is passed
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:110 # Money convertion raises an ExchangeRate::Unknown exception for unknown rates
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:119 # Money arithmetic allows * with a numeric
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:128 # Money arithmetic * with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:119 # Money arithmetic allows / with a numeric
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:128 # Money arithmetic / with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:136 # Money arithmetic + with numeric objects raises an ArgumentError
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:142 # Money arithmetic allows + with money with the same currency
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:151 # Money arithmetic raises Money::IncompatibleCurrencies for + with money with different currencies
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:136 # Money arithmetic - with numeric objects raises an ArgumentError
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:142 # Money arithmetic allows - with money with the same currency
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:151 # Money arithmetic raises Money::IncompatibleCurrencies for - with money with different currencies
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:159 # Money arithmetic + with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:159 # Money arithmetic - with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:159 # Money arithmetic * with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:159 # Money arithmetic / with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:168 # Money comparison works when currencies are the same
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:188 # Money comparison works for equality when currencies are the same
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:194 # Money comparison with <=> raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:200 # Money comparison with <=> raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:194 # Money comparison with < raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:200 # Money comparison with < raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:194 # Money comparison with <= raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:200 # Money comparison with <= raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:194 # Money comparison with > raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:200 # Money comparison with > raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:194 # Money comparison with >= raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-1h1u3n2/spec.rb:200 # Money comparison with >= raises IncompatibleCurrencies when currencies differ

История (1 версия и 0 коментара)

Ангел обнови решението на 16.01.2013 16:55 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ def initialize
+ @rates = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ @rates[[from_currency, to_currency]] = rate
+ end
+
+ def get(from_currency, to_currency)
+ @rates[[from_currency, to_currency]] or
+ 1 / @rates[[to_currency, from_currency]]
+ end
+end
+
+class Money
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ attr_reader :amount, :currency
+
+ def to_s
+ "#{@amount.truncate(2).to_s} #{@currency.to_s}"
+ end
+
+ def in(currency, exchange_rate)
+ rate = exchange_rate.get(@currency, currency)
+ if rate
+ currency * rate
+ else
+ nil
+ end
+ end
+end