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

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

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

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
Unknown = Class.new RuntimeError
def initialize
@currency_hash = {}
end
def set(from_currency, to_currency, rate)
if from_currency != to_currency
@currency_hash[[from_currency,to_currency]] = rate
reverse_rate = 1/rate
@currency_hash[[to_currency, from_currency]] = reverse_rate
end
end
def get(from_currency, to_currency)
if from_currency != to_currency
@currency_hash[[from_currency,to_currency]]
else
1.to_d
end
end
def convert(from_currency, to_currency, amount)
rate = get from_currency, to_currency
rate ? rate * amount : (raise Unknown)
end
end
class Money
include Comparable
attr_accessor :amount, :currency
IncompatibleCurrencies = Class.new ArgumentError
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def in(other_currency, exchange_rate)
new_amount = exchange_rate.convert(currency, other_currency, amount)
Money.new new_amount, other_currency
end
def to_s
amount_to_s = amount.round(2).to_s('F')
if amount_to_s =~ /\b\d*\.\d\b/
"#{amount_to_s}0 #{currency}"
else
"#{amount_to_s} #{currency}"
end
end
def <=>(other)
operation_with_other_money(:<=>, other)
end
def ==(other)
operation_with_other_money(:==, other)
end
def +(other)
Money.new operation_with_other_money(:+, other), currency
end
def -(other)
Money.new operation_with_other_money(:-, other), currency
end
def *(coefficient)
operation_with_numeric(:*,coefficient)
end
def /(coefficient)
operation_with_numeric(:/,coefficient)
end
private
def operation_with_numeric(operation, coefficient)
if coefficient.kind_of? Numeric
Money.new amount.send(operation, coefficient), currency
else
ArgumentError
end
end
def operation_with_other_money(operation, other)
if self.class != other.class
raise ArgumentError
elsif self.currency != other.currency
raise IncompatibleCurrencies
else
amount.send(operation, other.amount)
end
end
end

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

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

Failures:

  1) Money arithmetic * with money objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-tlbl85/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)>'

  2) Money arithmetic / with money objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-tlbl85/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)>'

  3) Money arithmetic * with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-tlbl85/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)>'

  4) Money arithmetic / with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-tlbl85/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)>'

Finished in 0.05726 seconds
47 examples, 4 failures

Failed examples:

rspec /tmp/d20130203-23049-tlbl85/spec.rb:128 # Money arithmetic * with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-tlbl85/spec.rb:128 # Money arithmetic / with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-tlbl85/spec.rb:159 # Money arithmetic * with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-tlbl85/spec.rb:159 # Money arithmetic / with other objects raises an ArgumentError

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

Мартин обнови решението на 10.01.2013 14:23 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ const_set("Unknown", Class.new(RuntimeError))
+
+ def initialize
+ @currency_hash = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ if from_currency != to_currency
+ @currency_hash[[from_currency,to_currency]] = rate
+ reverse_rate = 1/rate
+ @currency_hash[[to_currency, from_currency]] = reverse_rate
+ end
+ end
+
+ def get(from_currency, to_currency)
+ if from_currency != to_currency
+ @currency_hash[[from_currency,to_currency]]
+ else 1.to_d
+ end
+ end
+
+ def convert(from_currency, to_currency, amount)
+ rate = get from_currency, to_currency
+ rate ? rate * amount : (raise Unknown)
+ end
+end
+
+class Money
+ include Comparable
+ attr_accessor :amount, :currency
+
+ const_set("IncompatibleCurrencies", Class.new(ArgumentError))
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def in(_currency, exchange_rate)
+ new_amount = exchange_rate.convert(@currency, _currency, @amount)
+ Money.new new_amount, _currency
+ end
+
+ def to_s
+ "#{@amount.round(2).to_s('F')} #{@currency}"
+ end
+
+ def <=>(other)
+ operation_with_other_money(:<=>, other)
+ end
+
+ def +(other)
+ Money.new operation_with_other_money(:+, other), currency
+ end
+
+ def -(other)
+ Money.new operation_with_other_money(:-, other), currency
+ end
+
+ def *(coefficient)
+ operation_with_numeric(:*,coefficient)
+ end
+
+ def /(coefficient)
+ operation_with_numeric(:/,coefficient)
+ end
+
+ private
+
+ def operation_with_numeric(operation, coefficient)
+ if coefficient.kind_of? Numeric
+ Money.new amount.send(operation, coefficient), currency
+ else
+ ArgumentError
+ end
+ end
+
+ def operation_with_other_money(operation, other)
+ if self.class != other.class
+ raise ArgumentError
+ elsif currency != other.currency
+ raise IncompatibleCurrencies
+ else
+ amount.send(operation, other.amount)
+ end
+ end
+end

Мартин обнови решението на 11.01.2013 01:19 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
- const_set("Unknown", Class.new(RuntimeError))
+ Unknown = Class.new RuntimeError
def initialize
@currency_hash = {}
end
def set(from_currency, to_currency, rate)
if from_currency != to_currency
@currency_hash[[from_currency,to_currency]] = rate
reverse_rate = 1/rate
@currency_hash[[to_currency, from_currency]] = reverse_rate
end
end
def get(from_currency, to_currency)
if from_currency != to_currency
@currency_hash[[from_currency,to_currency]]
- else 1.to_d
+ else
+ 1.to_d
end
end
def convert(from_currency, to_currency, amount)
rate = get from_currency, to_currency
rate ? rate * amount : (raise Unknown)
end
end
class Money
include Comparable
attr_accessor :amount, :currency
- const_set("IncompatibleCurrencies", Class.new(ArgumentError))
+ IncompatibleCurrencies = Class.new ArgumentError
def initialize(amount, currency)
@amount = amount
@currency = currency
end
- def in(_currency, exchange_rate)
- new_amount = exchange_rate.convert(@currency, _currency, @amount)
- Money.new new_amount, _currency
+ def in(other_currency, exchange_rate)
+ new_amount = exchange_rate.convert(currency, other_currency, amount)
+ Money.new new_amount, other_currency
end
def to_s
- "#{@amount.round(2).to_s('F')} #{@currency}"
+ amount_to_s = amount.round(2).to_s('F')
+ if amount_to_s =~ /\b\d*\.\d\b/
+ "#{amount_to_s}0 #{currency}"
+ else
+ "#{amount_to_s} #{currency}"
+ end
end
def <=>(other)
operation_with_other_money(:<=>, other)
end
def +(other)
Money.new operation_with_other_money(:+, other), currency
end
def -(other)
Money.new operation_with_other_money(:-, other), currency
end
def *(coefficient)
operation_with_numeric(:*,coefficient)
end
def /(coefficient)
operation_with_numeric(:/,coefficient)
end
private
def operation_with_numeric(operation, coefficient)
if coefficient.kind_of? Numeric
Money.new amount.send(operation, coefficient), currency
else
ArgumentError
end
end
def operation_with_other_money(operation, other)
if self.class != other.class
raise ArgumentError
- elsif currency != other.currency
+ elsif self.currency != other.currency
raise IncompatibleCurrencies
else
amount.send(operation, other.amount)
end
end
end

Мартин обнови решението на 15.01.2013 17:59 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
Unknown = Class.new RuntimeError
def initialize
@currency_hash = {}
end
def set(from_currency, to_currency, rate)
if from_currency != to_currency
@currency_hash[[from_currency,to_currency]] = rate
reverse_rate = 1/rate
@currency_hash[[to_currency, from_currency]] = reverse_rate
end
end
def get(from_currency, to_currency)
if from_currency != to_currency
@currency_hash[[from_currency,to_currency]]
else
1.to_d
end
end
def convert(from_currency, to_currency, amount)
rate = get from_currency, to_currency
rate ? rate * amount : (raise Unknown)
end
end
class Money
include Comparable
attr_accessor :amount, :currency
IncompatibleCurrencies = Class.new ArgumentError
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def in(other_currency, exchange_rate)
new_amount = exchange_rate.convert(currency, other_currency, amount)
Money.new new_amount, other_currency
end
def to_s
amount_to_s = amount.round(2).to_s('F')
if amount_to_s =~ /\b\d*\.\d\b/
"#{amount_to_s}0 #{currency}"
else
"#{amount_to_s} #{currency}"
end
end
def <=>(other)
operation_with_other_money(:<=>, other)
end
+ def ==(other)
+ operation_with_other_money(:==, other)
+ end
+
def +(other)
Money.new operation_with_other_money(:+, other), currency
end
def -(other)
Money.new operation_with_other_money(:-, other), currency
end
def *(coefficient)
operation_with_numeric(:*,coefficient)
end
def /(coefficient)
operation_with_numeric(:/,coefficient)
end
private
def operation_with_numeric(operation, coefficient)
if coefficient.kind_of? Numeric
Money.new amount.send(operation, coefficient), currency
else
ArgumentError
end
end
def operation_with_other_money(operation, other)
if self.class != other.class
raise ArgumentError
elsif self.currency != other.currency
raise IncompatibleCurrencies
else
amount.send(operation, other.amount)
end
end
end