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

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

Към профила на Гергана Петрова

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
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 if from_currency == to_currency
rate = @rates.find { |rate| rate[0] == from_currency and rate[1] == to_currency }
rate ? rate[2] : nil
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
raise Unknown unless rate
rate * amount
end
end
class ExchangeRate::Unknown < RuntimeError
end
class Money
include Comparable
attr_accessor :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
"%.2f %s" % [amount, currency]
end
def in(convert_to, exchange_rate)
Money.new exchange_rate.convert(currency, convert_to, amount), convert_to
end
def *(other)
if other.is_a? Numeric
Money.new amount * other, currency
else
raise ArgumentError
end
end
def /(other)
if other.is_a? Numeric
Money.new amount / other, currency
else
raise ArgumentError
end
end
def +(other)
if other.currency != self.currency
raise IncompatibleCurrencies
else
Money.new amount + other.amount, currency
end
end
def -(other)
if other.currency != self.currency
raise IncompatibleCurrencies
else
Money.new amount - other.amount, currency
end
end
def <=>(other)
if not other.is_a? Money
raise ArgumentError
elsif other.currency != self.currency
raise IncompatibleCurrencies
else
amount <=> other.amount
end
end
end
class Money::IncompatibleCurrencies < ArgumentError
end

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

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

Failures:

  1) ExchangeRate#set allows rates to be updated if called multiple times
     Failure/Error: expect do
       result should have been changed to #<BigDecimal:a72e534,'0.149E1',18(18)>, but is now #<BigDecimal:a5f4a60,'0.15E1',18(18)>
     # /tmp/d20130203-23049-10c1ple/spec.rb:16: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) ExchangeRate#set sets the exchange rate in both directions
     Failure/Error: rate.get(:EUR, :BGN).should eq '1.25'.to_d
       
       expected: #<BigDecimal:a4f9d40,'0.125E1',18(18)>
            got: #<BigDecimal:a72d544,'0.2E1',9(36)>
       
       (compared using ==)
     # /tmp/d20130203-23049-10c1ple/spec.rb:25: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 numeric objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for 42:Fixnum>
     # /tmp/d20130203-23049-10c1ple/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)>'

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

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

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

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

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

Failed examples:

rspec /tmp/d20130203-23049-10c1ple/spec.rb:13 # ExchangeRate#set allows rates to be updated if called multiple times
rspec /tmp/d20130203-23049-10c1ple/spec.rb:21 # ExchangeRate#set sets the exchange rate in both directions
rspec /tmp/d20130203-23049-10c1ple/spec.rb:136 # Money arithmetic + with numeric objects raises an ArgumentError
rspec /tmp/d20130203-23049-10c1ple/spec.rb:136 # Money arithmetic - with numeric objects raises an ArgumentError
rspec /tmp/d20130203-23049-10c1ple/spec.rb:159 # Money arithmetic + with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-10c1ple/spec.rb:159 # Money arithmetic - with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-10c1ple/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-10c1ple/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ

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

Гергана обнови решението на 16.01.2013 10:52 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+
+ 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 if from_currency == to_currency
+ rate = @rates.find { |rate| rate[0] == from_currency and rate[1] == to_currency }
+ rate ? rate[2] : nil
+ end
+
+ def convert(from_currency, to_currency, amount)
+ rate = get(from_currency, to_currency)
+ raise Unknown unless rate
+ rate * amount
+ end
+end
+
+class ExchangeRate::Unknown < RuntimeError
+end
+
+class Money
+ attr_accessor :amount, :currency
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ "%.2f %s" % [amount, currency]
+ end
+
+ def in(convert_to, exchange_rate)
+ Money.new exchange_rate.convert(currency, convert_to, amount), convert_to
+ end
+end

Гергана обнови решението на 16.01.2013 11:59 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
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 if from_currency == to_currency
rate = @rates.find { |rate| rate[0] == from_currency and rate[1] == to_currency }
rate ? rate[2] : nil
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
raise Unknown unless rate
rate * amount
end
end
class ExchangeRate::Unknown < RuntimeError
end
class Money
attr_accessor :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
"%.2f %s" % [amount, currency]
end
def in(convert_to, exchange_rate)
Money.new exchange_rate.convert(currency, convert_to, amount), convert_to
end
-end
+
+ def *(other)
+ if other.is_a? Numeric
+ Money.new amount * other, currency
+ else
+ raise ArgumentError
+ end
+ end
+
+ def /(other)
+ if other.is_a? Numeric
+ Money.new amount / other, currency
+ else
+ raise ArgumentError
+ end
+ end
+
+ def +(other)
+ if other.currency != self.currency
+ raise IncompatibleCurrencies
+ else
+ Money.new amount + other.amount, currency
+ end
+ end
+
+ def -(other)
+ if other.currency != self.currency
+ raise IncompatibleCurrencies
+ else
+ Money.new amount - other.amount, currency
+ end
+ end
+end
+
+class Money::IncompatibleCurrencies
+end

Гергана обнови решението на 16.01.2013 12:01 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
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 if from_currency == to_currency
rate = @rates.find { |rate| rate[0] == from_currency and rate[1] == to_currency }
rate ? rate[2] : nil
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
raise Unknown unless rate
rate * amount
end
end
class ExchangeRate::Unknown < RuntimeError
end
class Money
attr_accessor :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
"%.2f %s" % [amount, currency]
end
def in(convert_to, exchange_rate)
Money.new exchange_rate.convert(currency, convert_to, amount), convert_to
end
def *(other)
if other.is_a? Numeric
Money.new amount * other, currency
else
raise ArgumentError
end
end
def /(other)
if other.is_a? Numeric
Money.new amount / other, currency
else
raise ArgumentError
end
end
def +(other)
if other.currency != self.currency
raise IncompatibleCurrencies
else
Money.new amount + other.amount, currency
end
end
def -(other)
if other.currency != self.currency
raise IncompatibleCurrencies
else
Money.new amount - other.amount, currency
end
end
end
-class Money::IncompatibleCurrencies
+class Money::IncompatibleCurrencies < ArgumentError
end

Гергана обнови решението на 16.01.2013 12:28 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
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 if from_currency == to_currency
rate = @rates.find { |rate| rate[0] == from_currency and rate[1] == to_currency }
rate ? rate[2] : nil
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
raise Unknown unless rate
rate * amount
end
end
class ExchangeRate::Unknown < RuntimeError
end
class Money
+ include Comparable
attr_accessor :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
"%.2f %s" % [amount, currency]
end
def in(convert_to, exchange_rate)
Money.new exchange_rate.convert(currency, convert_to, amount), convert_to
end
def *(other)
if other.is_a? Numeric
Money.new amount * other, currency
else
raise ArgumentError
end
end
def /(other)
if other.is_a? Numeric
Money.new amount / other, currency
else
raise ArgumentError
end
end
def +(other)
if other.currency != self.currency
raise IncompatibleCurrencies
else
Money.new amount + other.amount, currency
end
end
def -(other)
if other.currency != self.currency
raise IncompatibleCurrencies
else
Money.new amount - other.amount, currency
+ end
+ end
+
+ def <=>(other)
+ if not other.is_a? Money
+ raise ArgumentError
+ elsif other.currency != self.currency
+ raise IncompatibleCurrencies
+ else
+ amount <=> other.amount
end
end
end
class Money::IncompatibleCurrencies < ArgumentError
end