Решение на Шеста задача от Атанас Пройчев

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

Към профила на Атанас Пройчев

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 17 успешни тест(а)
  • 30 неуспешни тест(а)

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
def initialize
@rates = []
end
def set(*params)
if params[0].eql? params[1]
params[2] = 1.to_d
end
@rates << params.to_a
end
def get(from_currency, to_currency)
return 1.to_d if from_currency == to_currency
current_rate = []
current_rate << from_currency << to_currency
@rates.each do |rate|
return (rate + current_rate).uniq[2] if (rate + current_rate).uniq.length == 3
end
end
def convert(from_currency, to_currency, amount)
get(from_currency, to_currency) * amount
end
end
class Money
include Comparable
attr_reader :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
@amount.to_s('F2') + ' ' + @currency.to_s
end
def in(currency, rate)
Money.new rate.convert(@currency, currency, @amount), currency
end
def +(other)
Money.new @amount + other.amount, @currency if @currency.eql? other.currency
end
def *(other)
Money.new @amount * other, @currency
end
def /(other)
Money.new @amount / other, @currency
end
def -(other)
self + (other * -1) if @currency.eql? other.currency
end
def <=>(other)
if @currency.eql? other.currency
@amount <=> other.amount
else
false
end
end
end

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

..FFF.F..F.F...F..F.F.FF.FF.FFFFF..FFFFFFFFFFFF

Failures:

  1) ExchangeRate#set allows rates to be updated if called multiple times
     Failure/Error: expect do
       result should have been changed to #<BigDecimal:9f96150,'0.149E1',18(18)>, but is now #<BigDecimal:9e5ed78,'0.15E1',18(18)>
     # /tmp/d20130203-23049-1uwcsae/spec.rb:16: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#set sets the exchange rate in both directions
     Failure/Error: rate.get(:EUR, :BGN).should eq '1.25'.to_d
       
       expected: #<BigDecimal:9d62f50,'0.125E1',18(18)>
            got: #<BigDecimal:9f950c0,'0.2E1',9(36)>
       
       (compared using ==)
     # /tmp/d20130203-23049-1uwcsae/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)>'

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

  4) ExchangeRate#get returns the reversed rate between A and B, too
     Failure/Error: rate.get(:BGN, :EUR).should eq '0.5'.to_d
       
       expected: #<BigDecimal:9678708,'0.5E0',9(18)>
            got: #<BigDecimal:9678cf8,'0.2E1',9(18)>
       
       (compared using ==)
     # /tmp/d20130203-23049-1uwcsae/spec.rb:41: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-1uwcsae/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 B to A using an existing rate A -> B
     Failure/Error: rate.convert(:BGN, :EUR, 100.to_d).should eq 50.to_d
       
       expected: #<BigDecimal:97098e8,'0.5E2',9(36)>
            got: #<BigDecimal:9709a00,'0.2E3',9(36)>
       
       (compared using ==)
     # /tmp/d20130203-23049-1uwcsae/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)>'

  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-1uwcsae/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 raises an ExchangeRate::Unknown exception for unknown rates
     Failure/Error: end.to raise_error(ExchangeRate::Unknown)
     NameError:
       uninitialized constant ExchangeRate::Unknown
     # /tmp/d20130203-23049-1uwcsae/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)>'

  9) Money arithmetic * with money objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<TypeError: Money can't be coerced into BigDecimal>
     # /tmp/d20130203-23049-1uwcsae/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)>'

  10) Money arithmetic / with money objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<TypeError: Money can't be coerced into BigDecimal>
     # /tmp/d20130203-23049-1uwcsae/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)>'

  11) Money arithmetic + with numeric objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for 42:Fixnum>
     # /tmp/d20130203-23049-1uwcsae/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)>'

  12) 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-1uwcsae/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)>'

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

  14) 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-1uwcsae/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)>'

  15) Money arithmetic + with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for "foobar":String>
     # /tmp/d20130203-23049-1uwcsae/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)>'

  16) Money arithmetic - with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for "foobar":String>
     # /tmp/d20130203-23049-1uwcsae/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)>'

  17) Money arithmetic * with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<TypeError: String can't be coerced into BigDecimal>
     # /tmp/d20130203-23049-1uwcsae/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)>'

  18) Money arithmetic / with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<TypeError: String can't be coerced into BigDecimal>
     # /tmp/d20130203-23049-1uwcsae/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)>'

  19) Money comparison with <=> raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for :larodi:Symbol>
     # /tmp/d20130203-23049-1uwcsae/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)>'

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

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

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

  23) Money comparison with < raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for :larodi:Symbol>
     # /tmp/d20130203-23049-1uwcsae/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)>'

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

  25) Money comparison with <= raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for :larodi:Symbol>
     # /tmp/d20130203-23049-1uwcsae/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)>'

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

  27) Money comparison with > raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for :larodi:Symbol>
     # /tmp/d20130203-23049-1uwcsae/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-1uwcsae/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, got #<NoMethodError: undefined method `currency' for :larodi:Symbol>
     # /tmp/d20130203-23049-1uwcsae/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-1uwcsae/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.06975 seconds
47 examples, 30 failures

Failed examples:

rspec /tmp/d20130203-23049-1uwcsae/spec.rb:13 # ExchangeRate#set allows rates to be updated if called multiple times
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:21 # ExchangeRate#set sets the exchange rate in both directions
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:30 # ExchangeRate#get returns nil for non-existing rates
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:39 # ExchangeRate#get returns the reversed rate between A and B, too
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:55 # ExchangeRate#convert raises an ExchangeRate::Unknown exception when the rate is not defined
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:66 # ExchangeRate#convert converts from B to A using an existing rate A -> B
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:88 # Money has a custom to_s representation
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:110 # Money convertion raises an ExchangeRate::Unknown exception for unknown rates
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:128 # Money arithmetic * with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:128 # Money arithmetic / with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:136 # Money arithmetic + with numeric objects raises an ArgumentError
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:151 # Money arithmetic raises Money::IncompatibleCurrencies for + with money with different currencies
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:136 # Money arithmetic - with numeric objects raises an ArgumentError
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:151 # Money arithmetic raises Money::IncompatibleCurrencies for - with money with different currencies
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:159 # Money arithmetic + with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:159 # Money arithmetic - with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:159 # Money arithmetic * with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:159 # Money arithmetic / with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:194 # Money comparison with <=> raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:200 # Money comparison with <=> raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:194 # Money comparison with < raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:200 # Money comparison with < raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:194 # Money comparison with <= raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:200 # Money comparison with <= raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:194 # Money comparison with > raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:200 # Money comparison with > raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:194 # Money comparison with >= raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-1uwcsae/spec.rb:200 # Money comparison with >= raises IncompatibleCurrencies when currencies differ

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

Атанас обнови решението на 16.01.2013 13:33 (преди около 12 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ def initialize
+ @rates = []
+ end
+
+ def set(*params)
+ if params[0].eql? params[1]
+ params[2] = 1.to_d
+ end
+ @rates << params.to_a
+ end
+
+ def get(from_currency, to_currency)
+ return 1.to_d if from_currency == to_currency
+ current_rate = []
+ current_rate << from_currency << to_currency
+ @rates.each do |rate|
+ return (rate + current_rate).uniq[2] if (rate + current_rate).uniq.length == 3
+ end
+ end
+
+ def convert(from_currency, to_currency, amount)
+ get(from_currency, to_currency) * amount
+ end
+end
+
+class Money
+ include Comparable
+
+ attr_reader :amount, :currency
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ @amount.to_s('F2') + ' ' + @currency.to_s
+ end
+
+ def in(currency, rate)
+ Money.new rate.convert(@currency, currency, @amount), currency
+ end
+
+ def +(other)
+ Money.new @amount + other.amount, @currency if @currency.eql? other.currency
+ end
+
+ def *(other)
+ Money.new @amount * other, @currency
+ end
+
+ def /(other)
+ Money.new @amount / other, @currency
+ end
+
+ def -(other)
+ self + (other * -1) if @currency.eql? other.currency
+ end
+
+ def <=>(other)
+ if @currency.eql? other.currency
+ @amount <=> other.amount
+ else
+ false
+ end
+ end
+end