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

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

Към профила на Милан Миланов

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@rates = {}
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)
return 1.to_d if from_currency == to_currency
@rates[[from_currency, to_currency]]
end
def convert(from_currency, to_currency, amount)
raise Unknown unless @rates.has_key? [from_currency, to_currency]
amount * @rates[[from_currency, to_currency]]
end
end
class Money
include Comparable
attr_reader :amount, :currency
class IncompatibleCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def in(currency, exchange_rate)
amount = exchange_rate.convert @currency, currency, @amount
self.class.new amount, currency
end
def to_s
('%.2f' % @amount) + ' ' + currency.to_s
end
def ==(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless currency == other.currency
amount == other.amount
end
def <=>(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless currency == other.currency
amount <=> other.amount
end
def +(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless @currency == other.currency
self.class.new @amount + other.amount, @currency
end
def -(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless currency == other.currency
self.class.new @amount - other.amount, @currency
end
def *(number)
raise ArgumentError unless number.is_a? Numeric
self.class.new @amount * number, @currency
end
def /(number)
raise ArgumentError unless number.is_a? Numeric
self.class.new @amount / number, @currency
end
end

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

............F....F.............................

Failures:

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

  2) 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
     ExchangeRate::Unknown:
       ExchangeRate::Unknown
     # /tmp/d20130203-23049-1yxnixp/solution.rb:24:in `convert'
     # /tmp/d20130203-23049-1yxnixp/solution.rb:41:in `in'
     # /tmp/d20130203-23049-1yxnixp/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)>'

Finished in 0.07775 seconds
47 examples, 2 failures

Failed examples:

rspec /tmp/d20130203-23049-1yxnixp/spec.rb:71 # ExchangeRate#convert works for identical currencies without defining any rates
rspec /tmp/d20130203-23049-1yxnixp/spec.rb:106 # Money convertion does not change the amount if the same currency is passed

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

Милан обнови решението на 13.01.2013 17:14 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ def initialize
+ @rates = {}
+ @exchange = Struct.new(:first_currency, :second_currency)
+ end
+
+ def set(from_currency, to_currency, rate)
+ return if from_currency == to_currency
+
+ straight_exchange = @exchange.new from_currency, to_currency
+ reverse_exchange = @exchange.new to_currency, from_currency
+
+ @rates[straight_exchange] = rate
+ @rates[reverse_exchange] = 1 / rate
+ end
+
+ def get(from_currency, to_currency)
+ return BigDecimal.new('1') if from_currency == to_currency
+
+ exchange = @exchange.new from_currency, to_currency
+ @rates[exchange]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ exchange = @exchange.new from_currency, to_currency
+ raise Unknown unless @rates.has_key? exchange
+
+ amount * @rates[exchange]
+ end
+
+ class Unknown < RuntimeError
+ end
+end
+
+class Money
+ include Comparable
+ attr_reader :amount, :currency
+
+ def initialize(amount, currency)
+ @amount, @currency = amount, currency
+ end
+
+ def in(currency, exchange_rate)
+ amount = exchange_rate.convert @currency, currency, @amount
+ self.class.new amount, currency
+ end
+
+ def to_s
+ ('%.2f' % @amount) + ' ' + currency.to_s
+ end
+
+ def <=>(other)
+ raise ArgumentError unless other.is_a? Money
+ raise IncompatibleCurrencies unless currency == other.currency
+
+ amount <=> other.amount
+ end
+
+ def +(other)
+ raise ArgumentError unless other.is_a? Money
+ raise IncompatibleCurrencies unless @currency == other.currency
+
+ self.class.new @amount + other.amount, @currency
+ end
+
+ def -(other)
+ raise ArgumentError unless other.is_a? Money
+ raise IncompatibleCurrencies unless currency == other.currency
+
+ self.class.new @amount - other.amount, @currency
+ end
+
+ def *(number)
+ raise ArgumentError unless number.is_a? Numeric
+
+ self.class.new @amount * number, @currency
+ end
+
+ def /(number)
+ raise ArgumentError unless number.is_a? Numeric
+
+ self.class.new @amount / number, @currency
+ end
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+end

Милан обнови решението на 13.01.2013 21:25 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
+ class Unknown < RuntimeError
+ end
+
def initialize
@rates = {}
@exchange = Struct.new(:first_currency, :second_currency)
end
def set(from_currency, to_currency, rate)
return if from_currency == to_currency
-
straight_exchange = @exchange.new from_currency, to_currency
reverse_exchange = @exchange.new to_currency, from_currency
-
@rates[straight_exchange] = rate
@rates[reverse_exchange] = 1 / rate
end
def get(from_currency, to_currency)
return BigDecimal.new('1') if from_currency == to_currency
-
exchange = @exchange.new from_currency, to_currency
@rates[exchange]
end
def convert(from_currency, to_currency, amount)
exchange = @exchange.new from_currency, to_currency
raise Unknown unless @rates.has_key? exchange
-
amount * @rates[exchange]
end
-
- class Unknown < RuntimeError
- end
end
class Money
include Comparable
attr_reader :amount, :currency
+ class IncompatibleCurrencies < RuntimeError
+ end
+
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def in(currency, exchange_rate)
amount = exchange_rate.convert @currency, currency, @amount
self.class.new amount, currency
end
def to_s
('%.2f' % @amount) + ' ' + currency.to_s
end
def <=>(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless currency == other.currency
-
amount <=> other.amount
end
def +(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless @currency == other.currency
-
self.class.new @amount + other.amount, @currency
end
def -(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless currency == other.currency
-
self.class.new @amount - other.amount, @currency
end
def *(number)
raise ArgumentError unless number.is_a? Numeric
-
self.class.new @amount * number, @currency
end
def /(number)
raise ArgumentError unless number.is_a? Numeric
-
self.class.new @amount / number, @currency
- end
-
- class IncompatibleCurrencies < RuntimeError
end
end

Милан обнови решението на 14.01.2013 18:46 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@rates = {}
- @exchange = Struct.new(:first_currency, :second_currency)
end
- def set(from_currency, to_currency, rate)
+ def set(from_currency, to_currency, rate)
return if from_currency == to_currency
- straight_exchange = @exchange.new from_currency, to_currency
- reverse_exchange = @exchange.new to_currency, from_currency
- @rates[straight_exchange] = rate
- @rates[reverse_exchange] = 1 / rate
+ @rates[[from_currency, to_currency]] = rate
+ @rates[[to_currency, from_currency]] = 1 / rate
end
def get(from_currency, to_currency)
return BigDecimal.new('1') if from_currency == to_currency
- exchange = @exchange.new from_currency, to_currency
- @rates[exchange]
+ @rates[[from_currency, to_currency]]
end
def convert(from_currency, to_currency, amount)
- exchange = @exchange.new from_currency, to_currency
- raise Unknown unless @rates.has_key? exchange
- amount * @rates[exchange]
+ raise Unknown unless @rates.has_key? [from_currency, to_currency]
+ amount * @rates[[from_currency, to_currency]]
end
end
class Money
include Comparable
attr_reader :amount, :currency
class IncompatibleCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def in(currency, exchange_rate)
amount = exchange_rate.convert @currency, currency, @amount
self.class.new amount, currency
end
def to_s
('%.2f' % @amount) + ' ' + currency.to_s
end
def <=>(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless currency == other.currency
amount <=> other.amount
end
def +(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless @currency == other.currency
self.class.new @amount + other.amount, @currency
end
def -(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless currency == other.currency
self.class.new @amount - other.amount, @currency
end
def *(number)
raise ArgumentError unless number.is_a? Numeric
self.class.new @amount * number, @currency
end
def /(number)
raise ArgumentError unless number.is_a? Numeric
self.class.new @amount / number, @currency
end
end

Милан обнови решението на 15.01.2013 21:43 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@rates = {}
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)
- return BigDecimal.new('1') if from_currency == to_currency
+ return 1.to_d if from_currency == to_currency
@rates[[from_currency, to_currency]]
end
def convert(from_currency, to_currency, amount)
raise Unknown unless @rates.has_key? [from_currency, to_currency]
amount * @rates[[from_currency, to_currency]]
end
end
class Money
include Comparable
attr_reader :amount, :currency
class IncompatibleCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def in(currency, exchange_rate)
amount = exchange_rate.convert @currency, currency, @amount
self.class.new amount, currency
end
def to_s
('%.2f' % @amount) + ' ' + currency.to_s
end
def <=>(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless currency == other.currency
amount <=> other.amount
end
def +(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless @currency == other.currency
self.class.new @amount + other.amount, @currency
end
def -(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless currency == other.currency
self.class.new @amount - other.amount, @currency
end
def *(number)
raise ArgumentError unless number.is_a? Numeric
self.class.new @amount * number, @currency
end
def /(number)
raise ArgumentError unless number.is_a? Numeric
self.class.new @amount / number, @currency
end
end

Милан обнови решението на 15.01.2013 23:53 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@rates = {}
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)
return 1.to_d if from_currency == to_currency
@rates[[from_currency, to_currency]]
end
def convert(from_currency, to_currency, amount)
raise Unknown unless @rates.has_key? [from_currency, to_currency]
amount * @rates[[from_currency, to_currency]]
end
end
class Money
include Comparable
attr_reader :amount, :currency
class IncompatibleCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def in(currency, exchange_rate)
amount = exchange_rate.convert @currency, currency, @amount
self.class.new amount, currency
end
def to_s
('%.2f' % @amount) + ' ' + currency.to_s
end
+ def ==(other)
+ raise ArgumentError unless other.is_a? Money
+ raise IncompatibleCurrencies unless currency == other.currency
+ amount == other.amount
+ end
+
def <=>(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless currency == other.currency
amount <=> other.amount
end
def +(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless @currency == other.currency
self.class.new @amount + other.amount, @currency
end
def -(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless currency == other.currency
self.class.new @amount - other.amount, @currency
end
def *(number)
raise ArgumentError unless number.is_a? Numeric
self.class.new @amount * number, @currency
end
def /(number)
raise ArgumentError unless number.is_a? Numeric
self.class.new @amount / number, @currency
end
end