Решение на Шеста задача от Ивайло Христов

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

Към профила на Ивайло Христов

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
attr_reader :rates
def initialize()
@rates = {}
end
def set(first, second, rate)
@rates = @rates.merge({
first => { second => rate },
second => { first => BigDecimal.new('1')/rate },
})
end
def get(first, second)
return 1 if first == second
return @rates[first] && @rates[first][second]
end
def convert(first, second, amount)
rate = get(first, second)
if rate
rate * amount
else
raise Unknown
end
end
end
class Money
include Comparable
attr_reader :amount, :currency
class IncompatibleCurrencies < StandardError
end
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s()
"#{@amount.to_f.round(2)} #@currency"
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(@currency, currency, amount), currency
end
def compatible?(money)
if not money.is_a? Money
raise ArgumentError
elsif money.currency != self.currency
raise IncompatibleCurrencies
end
end
def *(amount)
Money.new @amount * amount.to_d, @currency
end
def /(amount)
Money.new @amount / amount.to_d, @currency
end
def +(money)
compatible?(money)
Money.new self.amount + money.amount, @currency
end
def -(money)
compatible?(money)
Money.new self.amount - money.amount, @currency
end
def <=>(money)
compatible?(money)
self.amount <=> money.amount
end
end

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

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

Failures:

  1) 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-1i34gg4/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)>'

  2) Money arithmetic * with money objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `to_d' for 5.0 USD:Money>
     # /tmp/d20130203-23049-1i34gg4/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 money objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `to_d' for 5.0 USD:Money>
     # /tmp/d20130203-23049-1i34gg4/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 other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-1i34gg4/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)>'

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

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

Failed examples:

rspec /tmp/d20130203-23049-1i34gg4/spec.rb:88 # Money has a custom to_s representation
rspec /tmp/d20130203-23049-1i34gg4/spec.rb:128 # Money arithmetic * with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-1i34gg4/spec.rb:128 # Money arithmetic / with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-1i34gg4/spec.rb:159 # Money arithmetic * with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1i34gg4/spec.rb:159 # Money arithmetic / with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1i34gg4/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-1i34gg4/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ

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

Ивайло обнови решението на 16.01.2013 16:46 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ class Unknown < RuntimeError
+ end
+
+ attr_reader :rates
+
+ def initialize()
+ @rates = {}
+ end
+
+ def set(first, second, rate)
+ @rates = @rates.merge({
+ first => { second => rate },
+ second => { first => BigDecimal.new('1')/rate },
+ })
+ end
+
+ def get(first, second)
+ return 1 if first == second
+ return @rates[first] && @rates[first][second]
+ end
+
+ def convert(first, second, amount)
+ rate = get(first, second)
+
+ if rate
+ rate * amount
+ else
+ raise Unknown
+ end
+ end
+end
+
+class Money
+ include Comparable
+ attr_reader :amount, :currency
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s()
+ "#{@amount.to_f.round(2)} #@currency"
+ end
+
+ def in(currency, exchange_rate)
+ Money.new exchange_rate.convert(@currency, currency, amount), currency
+ end
+
+ class IncompatibleCurrencies < StandardError
+ end
+
+ def compatible?(money)
+ if not money.is_a? Money
+ raise ArgumentError
+ elsif money.currency != self.currency
+ raise IncompatibleCurrencies
+ end
+ end
+
+ def *(amount)
+ Money.new @amount * amount.to_d, @currency
+ self
+ end
+
+ def /(amound)
+ Money.new @amount / amount.to_d, @currency
+ end
+
+ def +(money)
+ compatible?(money)
+ Money.new(self.amount + money.amount, @currency)
+ end
+
+ def -(money)
+ compatible?(money)
+ Money.new(self.amount + money.amount, @currency)
+ end
+
+ # def ==(money)
+ # compatible?(money)
+ # self.amount == money.amount
+ # end
+
+ # def !=(money)
+ # compatible?(money)
+ # self.amount != money.amount
+ # end
+
+ # def >(money)
+ # compatible?(money)
+ # self.amount > money.amount
+ # end
+
+ # def <(money)
+ # compatible?(money)
+ # self.amount < money.amount
+ # end
+
+ # def >=(money)
+ # compatible?(money)
+ # self.amount >= money.amount
+ # end
+
+ # def <=(money)
+ # compatible?(money)
+ # self.amount <= money.amount
+ # end
+
+ def <=>(money)
+ compatible?(money)
+ self.amount <=> money.amount
+ end
+end

Ивайло обнови решението на 16.01.2013 16:50 (преди над 11 години)

+#
+# Solution 6
+#
+
require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
attr_reader :rates
def initialize()
@rates = {}
end
def set(first, second, rate)
@rates = @rates.merge({
first => { second => rate },
second => { first => BigDecimal.new('1')/rate },
})
end
def get(first, second)
return 1 if first == second
return @rates[first] && @rates[first][second]
end
def convert(first, second, amount)
rate = get(first, second)
if rate
rate * amount
else
raise Unknown
end
end
end
class Money
include Comparable
attr_reader :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s()
"#{@amount.to_f.round(2)} #@currency"
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(@currency, currency, amount), currency
end
class IncompatibleCurrencies < StandardError
end
def compatible?(money)
if not money.is_a? Money
raise ArgumentError
elsif money.currency != self.currency
raise IncompatibleCurrencies
end
end
def *(amount)
Money.new @amount * amount.to_d, @currency
- self
end
def /(amound)
Money.new @amount / amount.to_d, @currency
end
def +(money)
compatible?(money)
Money.new(self.amount + money.amount, @currency)
end
def -(money)
compatible?(money)
Money.new(self.amount + money.amount, @currency)
end
- # def ==(money)
- # compatible?(money)
- # self.amount == money.amount
- # end
-
- # def !=(money)
- # compatible?(money)
- # self.amount != money.amount
- # end
-
- # def >(money)
- # compatible?(money)
- # self.amount > money.amount
- # end
-
- # def <(money)
- # compatible?(money)
- # self.amount < money.amount
- # end
-
- # def >=(money)
- # compatible?(money)
- # self.amount >= money.amount
- # end
-
- # def <=(money)
- # compatible?(money)
- # self.amount <= money.amount
- # end
-
def <=>(money)
compatible?(money)
self.amount <=> money.amount
end
-end
+end

Ивайло обнови решението на 16.01.2013 16:52 (преди над 11 години)

-#
-# Solution 6
-#
-
require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
attr_reader :rates
def initialize()
@rates = {}
end
def set(first, second, rate)
@rates = @rates.merge({
first => { second => rate },
second => { first => BigDecimal.new('1')/rate },
})
end
def get(first, second)
return 1 if first == second
return @rates[first] && @rates[first][second]
end
def convert(first, second, amount)
rate = get(first, second)
if rate
rate * amount
else
raise Unknown
end
end
end
class Money
include Comparable
attr_reader :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s()
"#{@amount.to_f.round(2)} #@currency"
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(@currency, currency, amount), currency
end
class IncompatibleCurrencies < StandardError
end
def compatible?(money)
if not money.is_a? Money
raise ArgumentError
elsif money.currency != self.currency
raise IncompatibleCurrencies
end
end
def *(amount)
Money.new @amount * amount.to_d, @currency
end
- def /(amound)
+ def /(amount)
Money.new @amount / amount.to_d, @currency
end
def +(money)
compatible?(money)
- Money.new(self.amount + money.amount, @currency)
+ Money.new self.amount + money.amount, @currency
end
def -(money)
compatible?(money)
- Money.new(self.amount + money.amount, @currency)
+ Money.new self.amount - money.amount, @currency
end
def <=>(money)
compatible?(money)
self.amount <=> money.amount
end
-end
+end

Ивайло обнови решението на 16.01.2013 16:54 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
attr_reader :rates
def initialize()
@rates = {}
end
def set(first, second, rate)
@rates = @rates.merge({
first => { second => rate },
second => { first => BigDecimal.new('1')/rate },
})
end
def get(first, second)
return 1 if first == second
return @rates[first] && @rates[first][second]
end
def convert(first, second, amount)
rate = get(first, second)
if rate
rate * amount
else
raise Unknown
end
end
end
class Money
include Comparable
attr_reader :amount, :currency
+ class IncompatibleCurrencies < StandardError
+ end
+
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s()
"#{@amount.to_f.round(2)} #@currency"
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(@currency, currency, amount), currency
- end
-
- class IncompatibleCurrencies < StandardError
end
def compatible?(money)
if not money.is_a? Money
raise ArgumentError
elsif money.currency != self.currency
raise IncompatibleCurrencies
end
end
def *(amount)
Money.new @amount * amount.to_d, @currency
end
def /(amount)
Money.new @amount / amount.to_d, @currency
end
def +(money)
compatible?(money)
Money.new self.amount + money.amount, @currency
end
def -(money)
compatible?(money)
Money.new self.amount - money.amount, @currency
end
def <=>(money)
compatible?(money)
self.amount <=> money.amount
end
end