Решение на Шеста задача от Александър Иванов

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

Към профила на Александър Иванов

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@rates = Hash.new do |hash, (from_currency, to_currency)|
from_currency == to_currency ? 1.to_d : nil
end
end
def set(from_currency, to_currency, rate)
return if from_currency == to_currency
@rates[[from_currency, to_currency]] = rate
@rates[[to_currency, from_currency]] = 1 / rate
end
def get(from_currency, to_currency)
@rates[[from_currency, to_currency]]
end
def convert(from_currency, to_currency, amount)
rate = @rates[[from_currency, to_currency]]
if rate
@rates[[from_currency, to_currency]] * amount
else
raise Unknown.new("rate between #{from_currency} and #{to_currency} is unknown")
end
end
end
class Money
include Comparable
attr_accessor :amount, :currency
class IncompatibleCurrencies < StandardError
end
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
"#{'%.2f' % @amount} #{@currency}"
end
def in(currency, exchange_rate)
Money.new(exchange_rate.convert(@currency, currency, @amount), currency)
end
def *(other)
raise ArgumentError if not Numeric === other
Money.new(@amount * other, currency)
end
def /(other)
raise ArgumentError if not Numeric === other
Money.new(@amount / other, currency)
end
def +(other)
raise ArgumentError if not Money === other
if @currency != other.currency
raise IncompatibleCurrencies.new("#@currency is not compatible with #{other.currency}")
else
Money.new(@amount + other.amount, @currency)
end
end
def -(other)
raise ArgumentError if not Money === other
if @currency != other.currency
raise IncompatibleCurrencies.new("#@currency is not compatible with #{other.currency}")
else
Money.new(@amount - other.amount, @currency)
end
end
def <=>(other)
if Money === other and @currency != other.currency
raise IncompatibleCurrencies.new("#@currency is not compatible with #{other.currency}")
elsif Money === other
@amount <=> other.amount
else
raise ArgumentError.new("expected Money got #{other.class}")
end
end
end

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

.....................................FF........

Failures:

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

  2) Money comparison with == raises IncompatibleCurrencies when currencies differ
     Failure/Error: expect do
       expected Money::IncompatibleCurrencies but nothing was raised
     # /tmp/d20130203-23049-76yh7n/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.05789 seconds
47 examples, 2 failures

Failed examples:

rspec /tmp/d20130203-23049-76yh7n/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-76yh7n/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ

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

Александър обнови решението на 16.01.2013 15:01 (преди около 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ class Unknown < RuntimeError
+ end
+
+ def initialize
+ @rates = Hash.new do |hash, (from_currency, to_currency)|
+ from_currency == to_currency ? 1.to_d : nil
+ end
+ end
+
+ def set(from_currency, to_currency, rate)
+ return if from_currency == to_currency
+ @rates[[from_currency, to_currency]] = rate
+ @rates[[to_currency, from_currency]] = 1 / rate
+ end
+
+ def get(from_currency, to_currency)
+ @rates[[from_currency, to_currency]]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ rate = @rates[[from_currency, to_currency]]
+ if rate
+ @rates[[from_currency, to_currency]] * amount
+ else
+ raise Unknown.new("rate between #{from_currency} and #{to_currency} is unknown")
+ end
+ end
+end
+
+
+class Money
+ include Comparable
+ attr_accessor :amount, :currency
+
+ class IncompatibleCurrencies < StandardError
+ end
+
+ def initialize(amount, currency)
+ @amount, @currency = amount, currency
+ end
+
+ def to_s
+ "#{'%.2f' % @amount} #{@currency}"
+ end
+
+ def in(currency, exchange_rate)
+ Money.new(exchange_rate.convert(@currency, currency, @amount), currency)
+ end
+
+ def *(other)
+ raise ArgumentError unless Numeric === other
+ Money.new(@amount * other, currency)
+ end
+
+ def /(other)
+ raise ArgumentError if not Numeric === other
+ Money.new(@amount / other, currency)
+ end
+
+ def +(other)
+ raise ArgumentError if not Money === other
+ if @currency != other.currency
+ raise IncompatibleCurrencies.new("#@currency is not compatible with #{other.currency}")
+ else
+ Money.new(@amount + other.amount, @currency)
+ end
+ end
+
+ def -(other)
+ raise ArgumentError if not Money === other
+ if @currency != other.currency
+ raise IncompatibleCurrencies.new("#@currency is not compatible with #{other.currency}")
+ else
+ Money.new(@amount - other.amount, @currency)
+ end
+ end
+
+ def <=>(other)
+ if Money === other and @currency != other.currency
+ raise IncompatibleCurrencies.new("#@currency is not compatible with #{other.currency}")
+ elsif Money === other
+ @amount <=> other.amount
+ else
+ raise ArgumentError.new("expected Money got #{other.class}")
+ end
+ end
+end

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

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@rates = Hash.new do |hash, (from_currency, to_currency)|
from_currency == to_currency ? 1.to_d : nil
end
end
def set(from_currency, to_currency, rate)
return if from_currency == to_currency
@rates[[from_currency, to_currency]] = rate
@rates[[to_currency, from_currency]] = 1 / rate
end
def get(from_currency, to_currency)
@rates[[from_currency, to_currency]]
end
def convert(from_currency, to_currency, amount)
rate = @rates[[from_currency, to_currency]]
if rate
@rates[[from_currency, to_currency]] * amount
else
raise Unknown.new("rate between #{from_currency} and #{to_currency} is unknown")
end
end
end
class Money
include Comparable
attr_accessor :amount, :currency
class IncompatibleCurrencies < StandardError
end
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
"#{'%.2f' % @amount} #{@currency}"
end
def in(currency, exchange_rate)
Money.new(exchange_rate.convert(@currency, currency, @amount), currency)
end
def *(other)
- raise ArgumentError unless Numeric === other
+ raise ArgumentError if not Numeric === other
Money.new(@amount * other, currency)
end
def /(other)
raise ArgumentError if not Numeric === other
Money.new(@amount / other, currency)
end
def +(other)
raise ArgumentError if not Money === other
if @currency != other.currency
raise IncompatibleCurrencies.new("#@currency is not compatible with #{other.currency}")
else
Money.new(@amount + other.amount, @currency)
end
end
def -(other)
raise ArgumentError if not Money === other
if @currency != other.currency
raise IncompatibleCurrencies.new("#@currency is not compatible with #{other.currency}")
else
Money.new(@amount - other.amount, @currency)
end
end
def <=>(other)
if Money === other and @currency != other.currency
raise IncompatibleCurrencies.new("#@currency is not compatible with #{other.currency}")
elsif Money === other
@amount <=> other.amount
else
raise ArgumentError.new("expected Money got #{other.class}")
end
end
end