Решение на Шеста задача от Станислав Гатев

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

Към профила на Станислав Гатев

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 43 успешни тест(а)
  • 4 неуспешни тест(а)

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@exchange_rates = {}
end
def set(from_currency, to_currency, rate)
@exchange_rates[hash_currency from_currency, to_currency] = rate
@exchange_rates[hash_currency to_currency, from_currency] = 1.to_d / rate
end
def get(from_currency, to_currency)
return 1.to_d if from_currency == to_currency
raise Unknown unless @exchange_rates.include? hash_currency from_currency, to_currency
@exchange_rates[hash_currency from_currency, to_currency]
end
def convert(from_currency, to_currency, amount)
amount * get(from_currency, to_currency)
end
private
def hash_currency(from_currency, to_currency)
"#{from_currency} -> #{to_currency}"
end
end
class Money
include Comparable
class IncompatibleCurrencies < RuntimeError
end
attr_reader :amount, :currency
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
"%.2f %.3s" % [amount.round(2).to_s('F'), currency]
end
def in(currency, exchange_rate)
new_amount = exchange_rate.convert @currency, currency, amount
Money.new new_amount, currency
end
def *(number)
raise ArgumentError unless number.kind_of? Numeric
Money.new amount * number, currency
end
def /(number)
raise ArgumentError unless number.kind_of? Numeric
Money.new amount / number, currency
end
def +(money)
raise ArgumentError unless money.kind_of? Money
raise IncompatibleCurrencies unless currency == money.currency
Money.new amount + money.amount, currency
end
def -(money)
raise ArgumentError unless money.kind_of? Money
raise IncompatibleCurrencies unless currency == money.currency
Money.new amount - money.amount, currency
end
def <=>(money)
raise ArgumentError unless money.kind_of? Money
raise IncompatibleCurrencies unless currency == money.currency
amount - money.amount
end
end

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

....F............................F...FF........

Failures:

  1) ExchangeRate#get returns nil for non-existing rates
     Failure/Error: rate.get(:EUR, :BGN).should be_nil
     ExchangeRate::Unknown:
       ExchangeRate::Unknown
     # /tmp/d20130203-23049-emrnms/solution.rb:20:in `get'
     # /tmp/d20130203-23049-emrnms/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) Money comparison works when currencies are the same
     Failure/Error: (a <=> b).should eq 1
       
       expected: 1
            got: #<BigDecimal:876cfd4,'0.149E2',18(36)>
       
       (compared using ==)
     # /tmp/d20130203-23049-emrnms/spec.rb:174: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) Money comparison with == raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-emrnms/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)>'

  4) Money comparison with == raises IncompatibleCurrencies when currencies differ
     Failure/Error: expect do
       expected Money::IncompatibleCurrencies but nothing was raised
     # /tmp/d20130203-23049-emrnms/spec.rb:201: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.05935 seconds
47 examples, 4 failures

Failed examples:

rspec /tmp/d20130203-23049-emrnms/spec.rb:30 # ExchangeRate#get returns nil for non-existing rates
rspec /tmp/d20130203-23049-emrnms/spec.rb:168 # Money comparison works when currencies are the same
rspec /tmp/d20130203-23049-emrnms/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-emrnms/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ

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

Станислав обнови решението на 11.01.2013 11:05 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+
+ class Unknown < RuntimeError
+ end
+
+ def initialize
+ @exchange_rates = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ @exchange_rates[hash from_currency, to_currency] = rate
+ @exchange_rates[hash to_currency, from_currency] = 1.to_d / rate
+ end
+
+ def get(from_currency, to_currency)
+ @exchange_rates[hash from_currency, to_currency]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ 1.to_d if from_currency == to_currency
+ amount * get(from_currency, to_currency)
+ end
+
+ private
+
+ def hash(from_currency, to_currency)
+ "#{from_currency} -> #{to_currency}"
+ end
+
+end
+
+class Money
+ include Comparable
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ attr_reader :amount, :currency
+
+ def initialize(amount, currency)
+ @amount, @currency = amount, currency
+ end
+
+ def to_s
+ "%.2f %.3s" % [amount.round(2).to_s('F'), currency]
+ end
+
+ def in(currency, exchange_rate)
+ new_amount = exchange_rate.convert @currency, currency, amount
+ Money.new new_amount, currency
+ end
+
+ def *(number)
+ raise ArgumentError unless number.kind_of? Numeric
+ Money.new amount * number, currency
+ end
+
+ def /(number)
+ raise ArgumentError unless number.kind_of? Numeric
+ Money.new amount / number, currency
+ end
+
+ def +(money)
+ raise ArgumentError unless money.kind_of? Money
+ raise IncompatibleCurrencies unless currency == money.currency
+ Money.new amount + money.amount, currency
+ end
+
+ def -(money)
+ raise ArgumentError unless money.kind_of? Money
+ raise IncompatibleCurrencies unless currency == money.currency
+ Money.new amount - money.amount, currency
+ end
+
+ def <=>(money)
+ raise ArgumentError unless money.kind_of? Money
+ raise IncompatibleCurrencies unless currency == money.currency
+ amount - money.amount
+ end
+
+end

Станислав обнови решението на 11.01.2013 11:49 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@exchange_rates = {}
end
def set(from_currency, to_currency, rate)
- @exchange_rates[hash from_currency, to_currency] = rate
- @exchange_rates[hash to_currency, from_currency] = 1.to_d / rate
+ @exchange_rates[hash_currency from_currency, to_currency] = rate
+ @exchange_rates[hash_currency to_currency, from_currency] = 1.to_d / rate
end
def get(from_currency, to_currency)
- @exchange_rates[hash from_currency, to_currency]
+ return 1.to_d if from_currency == to_currency
+ raise Unknown unless @exchange_rates.include? hash_currency from_currency, to_currency
+ @exchange_rates[hash_currency from_currency, to_currency]
end
def convert(from_currency, to_currency, amount)
- 1.to_d if from_currency == to_currency
amount * get(from_currency, to_currency)
end
private
- def hash(from_currency, to_currency)
+ def hash_currency(from_currency, to_currency)
"#{from_currency} -> #{to_currency}"
end
end
class Money
include Comparable
class IncompatibleCurrencies < RuntimeError
end
attr_reader :amount, :currency
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
"%.2f %.3s" % [amount.round(2).to_s('F'), currency]
end
def in(currency, exchange_rate)
new_amount = exchange_rate.convert @currency, currency, amount
Money.new new_amount, currency
end
def *(number)
raise ArgumentError unless number.kind_of? Numeric
Money.new amount * number, currency
end
def /(number)
raise ArgumentError unless number.kind_of? Numeric
Money.new amount / number, currency
end
def +(money)
raise ArgumentError unless money.kind_of? Money
raise IncompatibleCurrencies unless currency == money.currency
Money.new amount + money.amount, currency
end
def -(money)
raise ArgumentError unless money.kind_of? Money
raise IncompatibleCurrencies unless currency == money.currency
Money.new amount - money.amount, currency
end
def <=>(money)
raise ArgumentError unless money.kind_of? Money
raise IncompatibleCurrencies unless currency == money.currency
amount - money.amount
end
end