Решение на Шеста задача от Николай Димитров

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

Към профила на Николай Димитров

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize()
@rates = {}
end
def set(from_currency, to_currency, rate)
if from_currency != to_currency
@rates[[from_currency, to_currency]] = rate.to_d
@rates[[to_currency, from_currency]] = 1 / rate.to_d
end
end
def get(from_currency, to_currency)
if from_currency == to_currency
BigDecimal.new('1')
else
@rates[[from_currency, to_currency]]
end
end
def convert(from_currency, to_currency, amount)
if get(from_currency, to_currency)
amount * get(from_currency, to_currency)
else
raise Unknown
end
end
end
class Money
include Comparable
attr_accessor :amount, :currency
class IncompatibleCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
"%.2f %s" % [amount, currency]
end
def in(currency, exchange_rate)
exchange_rate.convert(self.currency, currency, amount)
end
def +(other)
raise IncompatibleCurrencies if currency != other.currency
Money.new(amount + other.amount, currency)
end
def -(other)
raise IncompatibleCurrencies if currency != other.currency
Money.new(amount - other.amount, currency)
end
def *(number)
Money.new(amount * number, currency)
end
def /(number)
Money.new(amount / number, currency)
end
def <=>(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies if currency != other.currency
amount <=> other.amount
end
def ==(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies if currency != other.currency
amount == other.amount
end
end

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

................FF..F.FF..F..FFFF..............

Failures:

  1) Money convertion allows convertion to other currencies
     Failure/Error: levas.amount.should eq '39.2'.to_d
     NoMethodError:
       undefined method `amount' for #<BigDecimal:9411a64,'0.392E2',18(45)>
     # /tmp/d20130203-23049-g4tjra/spec.rb:102: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
     NoMethodError:
       undefined method `amount' for #<BigDecimal:9571f80,'0.5E1',9(36)>
     # /tmp/d20130203-23049-g4tjra/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)>'

  3) 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-g4tjra/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)>'

  4) 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-g4tjra/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)>'

  5) 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-g4tjra/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)>'

  6) 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-g4tjra/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)>'

  7) 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-g4tjra/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)>'

  8) 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-g4tjra/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)>'

  9) 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-g4tjra/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)>'

  10) 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-g4tjra/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.05978 seconds
47 examples, 10 failures

Failed examples:

rspec /tmp/d20130203-23049-g4tjra/spec.rb:95 # Money convertion allows convertion to other currencies
rspec /tmp/d20130203-23049-g4tjra/spec.rb:106 # Money convertion does not change the amount if the same currency is passed
rspec /tmp/d20130203-23049-g4tjra/spec.rb:128 # Money arithmetic * with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-g4tjra/spec.rb:128 # Money arithmetic / with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-g4tjra/spec.rb:136 # Money arithmetic + with numeric objects raises an ArgumentError
rspec /tmp/d20130203-23049-g4tjra/spec.rb:136 # Money arithmetic - with numeric objects raises an ArgumentError
rspec /tmp/d20130203-23049-g4tjra/spec.rb:159 # Money arithmetic + with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-g4tjra/spec.rb:159 # Money arithmetic - with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-g4tjra/spec.rb:159 # Money arithmetic * with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-g4tjra/spec.rb:159 # Money arithmetic / with other objects raises an ArgumentError

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

Николай обнови решението на 10.01.2013 13:04 (преди почти 12 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ attr_accessor :rates
+
+ class Unknown < RuntimeError
+ end
+
+ def initialize()
+ @rates = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ if from_currency != to_currency
+ @rates[[from_currency, to_currency]] = rate
+ @rates[[to_currency, from_currency]] = 1/rate
+ end
+ end
+
+ def get(from_currency, to_currency)
+ if from_currency == to_currency
+ 1#BigDecimal.new(1)
+ else
+ @rates[[from_currency, to_currency]]
+ end
+ end
+
+ def convert(from_currency, to_currency, amount)
+ if get(from_currency, to_currency)
+ amount * get(from_currency, to_currency)
+ else
+ raise Unknown, 'Unknown exchange rate between these currencies'
+ end
+ end
+end
+
+class Money
+ attr_accessor :amount, :currency
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ def initialize(amount, currency)
+ @amount, @currency = amount, currency
+ end
+
+ def to_s
+ "%.2f %s" % [amount, currency]
+ end
+
+ def in(currency, exchange_rate)
+ exchange_rate.convert(self.currency, currency, amount)
+ end
+
+ def +(other)
+ raise IncompatibleCurrencies if currency != other.currency
+ Money.new(amount + other.amount, currency)
+ end
+
+ def -(other)
+ raise IncompatibleCurrencies if currency != other.currency
+ Money.new(amount - other.amount, currency)
+ end
+
+ def *(number)
+ Money.new(amount * number, currency)
+ end
+
+ def /(number)
+ Money.new(amount / number, currency)
+ end
+
+ def <(other)
+ amount < other.amount
+ end
+
+ def >(other)
+ amount > other.amount
+ end
+
+ def <=(other)
+ amount <= other.amount
+ end
+
+ def >=(other)
+ amount >= other.amount
+ end
+
+ def <=>(other)
+ amount <=> other.amount
+ end
+end

Николай обнови решението на 10.01.2013 14:09 (преди почти 12 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
- attr_accessor :rates
-
class Unknown < RuntimeError
end
def initialize()
@rates = {}
end
def set(from_currency, to_currency, rate)
if from_currency != to_currency
- @rates[[from_currency, to_currency]] = rate
- @rates[[to_currency, from_currency]] = 1/rate
+ @rates[[from_currency, to_currency]] = rate.to_d
+ @rates[[to_currency, from_currency]] = 1 / rate.to_d
end
end
def get(from_currency, to_currency)
if from_currency == to_currency
- 1#BigDecimal.new(1)
+ BigDecimal.new('1')
else
@rates[[from_currency, to_currency]]
end
end
def convert(from_currency, to_currency, amount)
if get(from_currency, to_currency)
amount * get(from_currency, to_currency)
else
- raise Unknown, 'Unknown exchange rate between these currencies'
+ raise Unknown, 'Unknown exchange rate between currencies'
end
end
end
class Money
attr_accessor :amount, :currency
+ COMPARISON_OPERATORS = [:<=>, :==, :<, :<=, :>, :>=]
+
class IncompatibleCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
"%.2f %s" % [amount, currency]
end
def in(currency, exchange_rate)
exchange_rate.convert(self.currency, currency, amount)
end
def +(other)
- raise IncompatibleCurrencies if currency != other.currency
+ raise_error_if_incompatible_currencies_with(other)
Money.new(amount + other.amount, currency)
end
def -(other)
- raise IncompatibleCurrencies if currency != other.currency
+ raise_error_if_incompatible_currencies_with(other)
Money.new(amount - other.amount, currency)
end
def *(number)
Money.new(amount * number, currency)
end
def /(number)
Money.new(amount / number, currency)
end
- def <(other)
- amount < other.amount
+ COMPARISON_OPERATORS.each do |comparator|
+ define_method(comparator) do |other|
+ raise_error_if_incompatible_currencies_with(other)
+ amount.send(comparator, other.amount)
+ end
end
- def >(other)
- amount > other.amount
- end
-
- def <=(other)
- amount <= other.amount
- end
-
- def >=(other)
- amount >= other.amount
- end
-
- def <=>(other)
- amount <=> other.amount
+ def raise_error_if_incompatible_currencies_with(other)
+ raise IncompatibleCurrencies if currency != other.currency
end
end

Николай обнови решението на 11.01.2013 09:16 (преди почти 12 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize()
@rates = {}
end
def set(from_currency, to_currency, rate)
if from_currency != to_currency
@rates[[from_currency, to_currency]] = rate.to_d
@rates[[to_currency, from_currency]] = 1 / rate.to_d
end
end
def get(from_currency, to_currency)
if from_currency == to_currency
BigDecimal.new('1')
else
@rates[[from_currency, to_currency]]
end
end
def convert(from_currency, to_currency, amount)
if get(from_currency, to_currency)
amount * get(from_currency, to_currency)
else
raise Unknown, 'Unknown exchange rate between currencies'
end
end
end
class Money
+ include Comparable
+
attr_accessor :amount, :currency
- COMPARISON_OPERATORS = [:<=>, :==, :<, :<=, :>, :>=]
-
class IncompatibleCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
"%.2f %s" % [amount, currency]
end
def in(currency, exchange_rate)
exchange_rate.convert(self.currency, currency, amount)
end
def +(other)
raise_error_if_incompatible_currencies_with(other)
Money.new(amount + other.amount, currency)
end
def -(other)
raise_error_if_incompatible_currencies_with(other)
Money.new(amount - other.amount, currency)
end
def *(number)
Money.new(amount * number, currency)
end
def /(number)
Money.new(amount / number, currency)
end
- COMPARISON_OPERATORS.each do |comparator|
- define_method(comparator) do |other|
- raise_error_if_incompatible_currencies_with(other)
- amount.send(comparator, other.amount)
- end
+ def <=>(other)
+ raise_error_if_incompatible_currencies_with(other)
+ amount <=> other.amount
end
+ private
+
def raise_error_if_incompatible_currencies_with(other)
raise IncompatibleCurrencies if currency != other.currency
end
-end
+end

Николай обнови решението на 15.01.2013 20:18 (преди почти 12 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize()
@rates = {}
end
def set(from_currency, to_currency, rate)
if from_currency != to_currency
@rates[[from_currency, to_currency]] = rate.to_d
@rates[[to_currency, from_currency]] = 1 / rate.to_d
end
end
def get(from_currency, to_currency)
if from_currency == to_currency
BigDecimal.new('1')
else
@rates[[from_currency, to_currency]]
end
end
def convert(from_currency, to_currency, amount)
if get(from_currency, to_currency)
amount * get(from_currency, to_currency)
else
- raise Unknown, 'Unknown exchange rate between currencies'
+ raise Unknown
end
end
end
class Money
include Comparable
attr_accessor :amount, :currency
class IncompatibleCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
"%.2f %s" % [amount, currency]
end
def in(currency, exchange_rate)
exchange_rate.convert(self.currency, currency, amount)
end
def +(other)
raise_error_if_incompatible_currencies_with(other)
Money.new(amount + other.amount, currency)
end
def -(other)
raise_error_if_incompatible_currencies_with(other)
Money.new(amount - other.amount, currency)
end
def *(number)
Money.new(amount * number, currency)
end
def /(number)
Money.new(amount / number, currency)
end
def <=>(other)
+ raise ArgumentError unless other.is_a? Money
raise_error_if_incompatible_currencies_with(other)
amount <=> other.amount
end
private
def raise_error_if_incompatible_currencies_with(other)
raise IncompatibleCurrencies if currency != other.currency
end
end

Николай обнови решението на 16.01.2013 12:08 (преди почти 12 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize()
@rates = {}
end
def set(from_currency, to_currency, rate)
if from_currency != to_currency
@rates[[from_currency, to_currency]] = rate.to_d
@rates[[to_currency, from_currency]] = 1 / rate.to_d
end
end
def get(from_currency, to_currency)
if from_currency == to_currency
BigDecimal.new('1')
else
@rates[[from_currency, to_currency]]
end
end
def convert(from_currency, to_currency, amount)
if get(from_currency, to_currency)
amount * get(from_currency, to_currency)
else
raise Unknown
end
end
end
class Money
include Comparable
attr_accessor :amount, :currency
class IncompatibleCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
"%.2f %s" % [amount, currency]
end
def in(currency, exchange_rate)
exchange_rate.convert(self.currency, currency, amount)
end
def +(other)
- raise_error_if_incompatible_currencies_with(other)
+ raise IncompatibleCurrencies if currency != other.currency
Money.new(amount + other.amount, currency)
end
def -(other)
- raise_error_if_incompatible_currencies_with(other)
+ raise IncompatibleCurrencies if currency != other.currency
Money.new(amount - other.amount, currency)
end
def *(number)
Money.new(amount * number, currency)
end
def /(number)
Money.new(amount / number, currency)
end
def <=>(other)
raise ArgumentError unless other.is_a? Money
- raise_error_if_incompatible_currencies_with(other)
+ raise IncompatibleCurrencies if currency != other.currency
amount <=> other.amount
end
- private
-
- def raise_error_if_incompatible_currencies_with(other)
+ def ==(other)
+ raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies if currency != other.currency
+ amount == other.amount
end
end