Решение на Шеста задача от Кирил Владимиров

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

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

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize()
@rates = {}
end
def set(from_currency, to_currency, rate)
@rates["#{from_currency}-#{to_currency}"] = rate if from_currency != to_currency
end
def get(from_currency, to_currency)
if @rates.has_key? "#{from_currency}-#{to_currency}"
@rates["#{from_currency}-#{to_currency}"]
elsif @rates.has_key? "#{to_currency}-#{from_currency}"
1 / @rates["#{to_currency}-#{from_currency}"]
end
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
if from_currency == to_currency then amount
elsif rate then amount * rate
else raise Unknown
end
end
end
class Money
include Comparable
class IncompatibleCurrencies < RuntimeError
end
attr_reader :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
"#{@amount.truncate(2).to_digits} #{@currency}"
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(@currency, currency, @amount), currency
end
def <=>(other)
if other.is_a? self.class
if @currency == other.currency then @amount <=> other.amount
else raise IncompatibleCurrencies
end
else
raise ArgumentError
end
end
def +(other)
if other.is_a? self.class
if @currency == other.currency then Money.new(@amount + other.amount, @currency)
else raise IncompatibleCurrencies
end
else
raise ArgumentError
end
end
def -(other)
self + (Money.new(-other.amount, other.currency))
end
def *(other)
if other.is_a? Numeric
Money.new(@amount * other, @currency)
else
raise ArgumentError
end
end
def /(other)
self * (1.0/other)
end
alias eql? ==
end

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

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

Failures:

  1) ExchangeRate#set sets the exchange rate in both directions
     Failure/Error: rate.get(:EUR, :BGN).should eq '1.25'.to_d
       
       expected: #<BigDecimal:893a0a0,'0.125E1',18(18)>
            got: #<BigDecimal:8b6c83c,'0.2E1',9(36)>
       
       (compared using ==)
     # /tmp/d20130203-23049-hwpvx2/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)>'

  2) ExchangeRate#get always returns 1 as the exchange rate between two identical currencies
     Failure/Error: rate.get(:JPY, :JPY).should eq 1.to_d
       
       expected: #<BigDecimal:8255a28,'0.1E1',9(36)>
            got: nil
       
       (compared using ==)
     # /tmp/d20130203-23049-hwpvx2/spec.rb:45: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) ExchangeRate#get does not allow changing the exchange rate between two identical currencies
     Failure/Error: rate.get(:EUR, :EUR).should eq 1.to_d
       
       expected: #<BigDecimal:8258de0,'0.1E1',9(36)>
            got: nil
       
       (compared using ==)
     # /tmp/d20130203-23049-hwpvx2/spec.rb:50: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)>'

  4) Money has a custom to_s representation
     Failure/Error: Money.new('12.1'.to_d, :USD).to_s.should eq '12.10 USD'
       
       expected: "12.10 USD"
            got: "12.1 USD"
       
       (compared using ==)
     # /tmp/d20130203-23049-hwpvx2/spec.rb:90:in `block (2 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 allows / with a numeric
     Failure/Error: result.amount.should eq bucks.amount.public_send(operation, numeric)
       
       expected: #<BigDecimal:863efb8,'0.1190476190 47619048E0',18(36)>
            got: 0.11904761904761904
       
       (compared using ==)
     # /tmp/d20130203-23049-hwpvx2/spec.rb:125: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 money objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<TypeError: Money can't be coerced into Float>
     # /tmp/d20130203-23049-hwpvx2/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)>'

  7) Money arithmetic - with numeric objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `amount' for 42:Fixnum>
     # /tmp/d20130203-23049-hwpvx2/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)>'

  8) Money arithmetic - with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `amount' for "foobar":String>
     # /tmp/d20130203-23049-hwpvx2/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 Float>
     # /tmp/d20130203-23049-hwpvx2/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 comparison with == raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-hwpvx2/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)>'

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

Failed examples:

rspec /tmp/d20130203-23049-hwpvx2/spec.rb:21 # ExchangeRate#set sets the exchange rate in both directions
rspec /tmp/d20130203-23049-hwpvx2/spec.rb:44 # ExchangeRate#get always returns 1 as the exchange rate between two identical currencies
rspec /tmp/d20130203-23049-hwpvx2/spec.rb:48 # ExchangeRate#get does not allow changing the exchange rate between two identical currencies
rspec /tmp/d20130203-23049-hwpvx2/spec.rb:88 # Money has a custom to_s representation
rspec /tmp/d20130203-23049-hwpvx2/spec.rb:119 # Money arithmetic allows / with a numeric
rspec /tmp/d20130203-23049-hwpvx2/spec.rb:128 # Money arithmetic / with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-hwpvx2/spec.rb:136 # Money arithmetic - with numeric objects raises an ArgumentError
rspec /tmp/d20130203-23049-hwpvx2/spec.rb:159 # Money arithmetic - with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-hwpvx2/spec.rb:159 # Money arithmetic / with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-hwpvx2/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-hwpvx2/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ

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

Кирил обнови решението на 16.01.2013 15:46 (преди почти 12 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ class Unknown < RuntimeError
+ end
+
+ def initialize()
+ @rates = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ @rates["#{from_currency}-#{to_currency}"] = rate if from_currency != to_currency
+ end
+
+ def get(from_currency, to_currency)
+ if @rates.has_key? "#{from_currency}-#{to_currency}"
+ @rates["#{from_currency}-#{to_currency}"]
+ elsif @rates.has_key? "#{to_currency}-#{from_currency}"
+ 1. / @rates["#{to_currency}-#{from_currency}"]
+ end
+ end
+
+ def convert(from_currency, to_currency, amount)
+ rate = get(from_currency, to_currency)
+ if from_currency == to_currency then amount
+ elsif rate then amount * rate
+ else raise Unknown
+ end
+ end
+end
+
+class Money
+ include Comparable
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ attr_reader :amount, :currency
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ "#{@amount.truncate(2).to_digits} #{@currency}"
+ end
+
+ def in(currency, exchange_rate)
+ Money.new exchange_rate.convert(@currency, currency, @amount), currency
+ end
+
+ def <=>(other)
+ if other.is_a? self.class
+ if @currency == other.currency then @amount <=> other.amount
+ else raise IncompatibleCurrencies
+ end
+ else
+ raise ArgumentError
+ end
+ end
+
+ def +(other)
+ if other.is_a? self.class
+ if @currency == other.currency then Money.new(@amount + other.amount, @currency)
+ else raise IncompatibleCurrencies
+ end
+ else
+ raise ArgumentError
+ end
+ end
+
+ def -(other)
+ self + (Money.new(-other.amount, other.currency))
+ end
+
+ def *(other)
+ if other.is_a? Numeric
+ Money.new(@amount * other, @currency)
+ else
+ raise ArgumentError
+ end
+ end
+
+ def /(other)
+ self * (1.0/other)
+ end
+
+ alias eql? ==
+end

Кирил обнови решението на 16.01.2013 15:46 (преди почти 12 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize()
@rates = {}
end
def set(from_currency, to_currency, rate)
@rates["#{from_currency}-#{to_currency}"] = rate if from_currency != to_currency
end
def get(from_currency, to_currency)
if @rates.has_key? "#{from_currency}-#{to_currency}"
@rates["#{from_currency}-#{to_currency}"]
elsif @rates.has_key? "#{to_currency}-#{from_currency}"
- 1. / @rates["#{to_currency}-#{from_currency}"]
+ 1 / @rates["#{to_currency}-#{from_currency}"]
end
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
if from_currency == to_currency then amount
elsif rate then amount * rate
else raise Unknown
end
end
end
class Money
include Comparable
class IncompatibleCurrencies < RuntimeError
end
attr_reader :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
"#{@amount.truncate(2).to_digits} #{@currency}"
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(@currency, currency, @amount), currency
end
def <=>(other)
if other.is_a? self.class
if @currency == other.currency then @amount <=> other.amount
else raise IncompatibleCurrencies
end
else
raise ArgumentError
end
end
def +(other)
if other.is_a? self.class
if @currency == other.currency then Money.new(@amount + other.amount, @currency)
else raise IncompatibleCurrencies
end
else
raise ArgumentError
end
end
def -(other)
self + (Money.new(-other.amount, other.currency))
end
def *(other)
if other.is_a? Numeric
Money.new(@amount * other, @currency)
else
raise ArgumentError
end
end
def /(other)
self * (1.0/other)
end
alias eql? ==
end