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

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

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

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
def initialize
@rate = Hash.new { |h,k| h[k] = {} }
end
def set(from_currency, to_currency, rate)
return if from_currency == to_currency
@rate[from_currency][to_currency] = rate
@rate[to_currency][from_currency] = (1 / rate)
@rate[from_currency][from_currency] = 1
@rate[to_currency][to_currency] = 1
end
def get(from_currency, to_currency)
nil if @rate[from_currency][to_currency] == nil
BigDecimal.new(@rate[from_currency][to_currency].to_s)
end
def convert(from_currency, to_currency, amount)
if @rate[from_currency][to_currency] == nil
raise RuntimeError.new('ExchangeRate::unknow')
end
BigDecimal.new((@rate[from_currency][to_currency] * amount).to_s)
end
end
module Operations
end
class Money
include Operations
def initialize(amount,currency)
@amount,@currency = amount.round(2), currency
end
def to_s
@amount.to_digits + ' ' + @currency.to_s
end
def in(currency, exchange_rate)
rate = ExchangeRate.new
rate.set(@currency, currency, exchange_rate)
Money.new(rate.convert(@currency,currency,@amount) ,currency)
end
end

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

....F..FFF..F.FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

Failures:

  1) ExchangeRate#get returns nil for non-existing rates
     Failure/Error: rate.get(:EUR, :BGN).should be_nil
       expected: nil
            got: #<BigDecimal:a3ad6bc,'0.0',9(9)>
     # /tmp/d20130203-23049-1ou2ot7/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)>'

  2) ExchangeRate#get always returns 1 as the exchange rate between two identical currencies
     Failure/Error: rate.get(:JPY, :JPY).should eq 1.to_d
       
       expected: #<BigDecimal:9cb6e44,'0.1E1',9(36)>
            got: #<BigDecimal:9cb6fd4,'0.0',9(9)>
       
       (compared using ==)
     # /tmp/d20130203-23049-1ou2ot7/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)>'

  3) 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:9cc10b0,'0.1E1',9(36)>
            got: #<BigDecimal:9cc11f0,'0.0',9(9)>
       
       (compared using ==)
     # /tmp/d20130203-23049-1ou2ot7/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)>'

  4) 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-1ou2ot7/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)>'

  5) ExchangeRate#convert works for identical currencies without defining any rates
     Failure/Error: rate.convert(:JPY, :JPY, 123.to_d).should eq 123.to_d
     RuntimeError:
       ExchangeRate::unknow
     # /tmp/d20130203-23049-1ou2ot7/solution.rb:24:in `convert'
     # /tmp/d20130203-23049-1ou2ot7/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)>'

  6) Money has accessors for amount and currency
     Failure/Error: price.amount.should eq 42.to_d
     NoMethodError:
       undefined method `amount' for 42.0 USD:Money
     # /tmp/d20130203-23049-1ou2ot7/spec.rb:84: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)>'

  7) Money has a custom to_s representation
     Failure/Error: Money.new('12.1'.to_d, :USD).to_s.should eq '12.10 USD'
       
       expected: "12.10 USD"
            got: "12.1 USD"
       
       (compared using ==)
     # /tmp/d20130203-23049-1ou2ot7/spec.rb:90: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)>'

  8) Money convertion allows convertion to other currencies
     Failure/Error: levas = euros.in(:BGN, rate)
     TypeError:
       ExchangeRate can't be coerced into Fixnum
     # /tmp/d20130203-23049-1ou2ot7/solution.rb:12:in `/'
     # /tmp/d20130203-23049-1ou2ot7/solution.rb:12:in `set'
     # /tmp/d20130203-23049-1ou2ot7/solution.rb:46:in `in'
     # /tmp/d20130203-23049-1ou2ot7/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)>'

  9) 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
     RuntimeError:
       ExchangeRate::unknow
     # /tmp/d20130203-23049-1ou2ot7/solution.rb:24:in `convert'
     # /tmp/d20130203-23049-1ou2ot7/solution.rb:47:in `in'
     # /tmp/d20130203-23049-1ou2ot7/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)>'

  10) 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-1ou2ot7/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)>'

  11) Money arithmetic allows * with a numeric
     Failure/Error: result = bucks.public_send(operation, numeric)
     NoMethodError:
       undefined method `*' for 5.0 USD:Money
     # /tmp/d20130203-23049-1ou2ot7/spec.rb:123:in `public_send'
     # /tmp/d20130203-23049-1ou2ot7/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)>'

  12) Money arithmetic * with money objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `*' for 10.0 USD:Money>
     # /tmp/d20130203-23049-1ou2ot7/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)>'

  13) Money arithmetic allows / with a numeric
     Failure/Error: result = bucks.public_send(operation, numeric)
     NoMethodError:
       undefined method `/' for 5.0 USD:Money
     # /tmp/d20130203-23049-1ou2ot7/spec.rb:123:in `public_send'
     # /tmp/d20130203-23049-1ou2ot7/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 10.0 USD:Money>
     # /tmp/d20130203-23049-1ou2ot7/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 + with numeric objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `+' for 10.0 USD:Money>
     # /tmp/d20130203-23049-1ou2ot7/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)>'

  16) Money arithmetic allows + with money with the same currency
     Failure/Error: result = a.public_send(operation, b)
     NoMethodError:
       undefined method `+' for 15.0 EUR:Money
     # /tmp/d20130203-23049-1ou2ot7/spec.rb:146:in `public_send'
     # /tmp/d20130203-23049-1ou2ot7/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)>'

  17) 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-1ou2ot7/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)>'

  18) Money arithmetic - with numeric objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `-' for 10.0 USD:Money>
     # /tmp/d20130203-23049-1ou2ot7/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)>'

  19) Money arithmetic allows - with money with the same currency
     Failure/Error: result = a.public_send(operation, b)
     NoMethodError:
       undefined method `-' for 15.0 EUR:Money
     # /tmp/d20130203-23049-1ou2ot7/spec.rb:146:in `public_send'
     # /tmp/d20130203-23049-1ou2ot7/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)>'

  20) 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-1ou2ot7/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)>'

  21) Money arithmetic + with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `+' for 10.0 USD:Money>
     # /tmp/d20130203-23049-1ou2ot7/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)>'

  22) Money arithmetic - with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `-' for 10.0 USD:Money>
     # /tmp/d20130203-23049-1ou2ot7/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)>'

  23) Money arithmetic * with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `*' for 10.0 USD:Money>
     # /tmp/d20130203-23049-1ou2ot7/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 10.0 USD:Money>
     # /tmp/d20130203-23049-1ou2ot7/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 comparison works when currencies are the same
     Failure/Error: (a < b).should be_false
     NoMethodError:
       undefined method `<' for 12.45 USD:Money
     # /tmp/d20130203-23049-1ou2ot7/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)>'

  26) 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-1ou2ot7/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)>'

  27) Money comparison with <=> raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-1ou2ot7/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)>'

  28) Money comparison with <=> raises IncompatibleCurrencies when currencies differ
     Failure/Error: end.to raise_error(Money::IncompatibleCurrencies)
     NameError:
       uninitialized constant Money::IncompatibleCurrencies
     # /tmp/d20130203-23049-1ou2ot7/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)>'

  29) Money comparison with == raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-1ou2ot7/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-1ou2ot7/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, got #<NoMethodError: undefined method `<' for 12.0 USD:Money>
     # /tmp/d20130203-23049-1ou2ot7/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-1ou2ot7/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 12.0 USD:Money>
     # /tmp/d20130203-23049-1ou2ot7/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-1ou2ot7/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 12.0 USD:Money>
     # /tmp/d20130203-23049-1ou2ot7/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-1ou2ot7/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 12.0 USD:Money>
     # /tmp/d20130203-23049-1ou2ot7/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-1ou2ot7/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.07114 seconds
47 examples, 38 failures

Failed examples:

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

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

Николай обнови решението на 16.01.2013 16:49 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ def initialize
+ @rate = Hash.new { |h,k| h[k] = {} }
+ end
+
+ def set(from_currency, to_currency, rate)
+ return if from_currency == to_currency
+ @rate[from_currency][to_currency] = rate
+ @rate[to_currency][from_currency] = (1 / rate)
+ @rate[from_currency][from_currency] = 1
+ @rate[to_currency][to_currency] = 1
+ end
+
+ def get(from_currency, to_currency)
+ nil if @rate[from_currency][to_currency] == nil
+ BigDecimal.new(@rate[from_currency][to_currency].to_s)
+ end
+
+ def convert(from_currency, to_currency, amount)
+ if @rate[from_currency][to_currency] == nil
+ raise RuntimeError.new('ExchangeRate::unknow')
+ end
+ BigDecimal.new((@rate[from_currency][to_currency] * amount).to_s)
+ end
+end
+
+module Operations
+
+end
+
+class Money
+ include Operations
+ def initialize(amount,currency)
+ @amount,@currency = amount.round(2), currency
+ end
+
+ def to_s
+ @amount.to_digits + ' ' + @currency.to_s
+ end
+
+ def in(currency, exchange_rate)
+ rate = ExchangeRate.new
+ rate.set(@currency, currency, exchange_rate)
+ Money.new(rate.convert(@currency,currency,@amount) ,currency)
+ end
+end