Илиян обнови решението на 15.01.2013 20:25 (преди около 12 години)
+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ class Unknown < RuntimeError
+ end
+
+ attr_accessor :rates
+
+ def initialize
+ @rates={}
+ end
+
+ def key(from_currency, to_currency)
+ from_currency.to_s + to_currency.to_s
+ end
+
+ def set(from_currency, to_currency, rate)
+ @rates[key(from_currency, to_currency)] = rate
+ @rates[key(to_currency, from_currency)] = 1/rate #BigDecimal.new (1/rate)
+ end
+
+ def get(from_currency, to_currency)
+ @rates[key(from_currency, to_currency)]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ if not @rates.key?(key(from_currency,to_currency)) and from_currency != to_currency then
+ raise ExchangeRate::Unknown
+ elsif from_currency == to_currency then
+ amount
+ else
+ amount*get(from_currency, to_currency)
+ end
+ end
+end
+
+class Money
+ include Comparable
+
+ class IncompatibleCurrencies < ArgumentError
+ end
+
+ attr_accessor :amount, :currency
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ result = @amount.to_f.round(2).to_s
+ if result[-2] == '.' then
+ result += '0'
+ end
+ result + ' ' + @currency.to_s
+ end
+
+ def in(currency, exchange_rate)
+ Money.new exchange_rate.convert(@currency, currency, @amount), currency
+ end
+
+ def *(other)
+ Money.new @amount*other, @currency
+ end
+
+ def /(other)
+ Money.new @amount/other, @currency
+ end
+
+ def +(other)
+ if other.class != Money then raise ArgumentError end
+ if other.currency != @currency then raise Money::IncompatibleCurrencies end
+ Money.new @amount + other.amount, @currency
+ end
+
+ def -(other)
+ if other.class != Money then raise ArgumentError end
+ if other.currency != @currency then raise Money::IncompatibleCurrencies end
+ Money.new @amount - other.amount, @currency
+ end
+
+ def <=>(other)
+ if other.class != Money then raise ArgumentError end
+ if other.currency != @currency then raise Money::IncompatibleCurrencies end
+ @amount <=> other.amount
+ end
+end