Мартина обнови решението на 14.01.2013 20:23 (преди почти 12 години)
+class CurrencyPair
+ attr_reader :from_currency, :to_currency
+
+ def initialize(from_currency, to_currency)
+ @from_currency, @to_currency = from_currency, to_currency
+ end
+
+ def ==(other)
+ @from_currency == other.from_currency and
+ @to_currency == other.to_currency
+ end
+
+ alias eql? ==
+
+ def hash
+ [@from_currency, @to_currency].hash
+ end
+end
+
+class ExchangeRate
+ require 'bigdecimal'
+ require 'bigdecimal/util'
+
+ class Unknown < RuntimeError
+ end
+
+ def initialize
+ @quotations = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ @quotations[CurrencyPair.new(from_currency, to_currency)] = rate
+ @quotations[CurrencyPair.new(to_currency, from_currency)] = 1.quo(rate)
+ end
+
+ def get(from_currency, to_currency)
+ if from_currency == to_currency
+ '1'.to_d
+ else
+ @quotations[CurrencyPair.new(from_currency, to_currency)]
+ end
+ end
+
+ def convert(from_currency, to_currency, amount)
+ rate = get(from_currency, to_currency)
+ raise Unknown if rate.nil?
+ BigDecimal.new(amount * rate)
+ end
+end
+
+class Money
+ require 'bigdecimal'
+ require 'bigdecimal/util'
+
+ attr_reader :amount, :currency
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ def initialize(amount, currency)
+ @amount, @currency = amount, currency
+ end
+
+ def to_s
+ "#{amount.ceil(2).to_digits} #{currency}"
+ end
+
+ def in(to_currency, exchange_rate)
+ new_amount = exchange_rate.convert(currency, to_currency, amount)
+ Money.new(new_amount, to_currency)
+ end
+
+ def *(other)
+ raise ArgumentError unless other.kind_of?(Numeric)
+ new_amount = other * amount
+ Money.new(new_amount, currency)
+ end
+
+ def /(other)
+ raise ArgumentError unless other.kind_of?(Numeric)
+ new_amount = amount / other
+ Money.new(new_amount, currency)
+ end
+
+ def +(other)
+ raise ArgumentError unless other.kind_of?(Money)
+ raise IncompatibleCurrencies unless currency == other.currency
+ new_amount = amount + other.amount
+ Money.new(new_amount, currency)
+ end
+
+ def -(other)
+ raise ArgumentError unless other.kind_of?(Money)
+ raise IncompatibleCurrencies unless currency == other.currency
+ new_amount = amount - other.amount
+ Money.new(new_amount, currency)
+ end
+
+ def <(other)
+ raise ArgumentError unless other.kind_of?(Money)
+ raise IncompatibleCurrencies unless currency == other.currency
+ amount < other.amount
+ end
+
+ def <=(other)
+ raise ArgumentError unless other.kind_of?(Money)
+ raise IncompatibleCurrencies unless currency == other.currency
+ amount <= other.amount
+ end
+
+ def >=(other)
+ raise ArgumentError unless other.kind_of?(Money)
+ raise IncompatibleCurrencies unless currency == other.currency
+ amount >= other.amount
+ end
+
+ def ==(other)
+ raise ArgumentError unless other.kind_of?(Money)
+ raise IncompatibleCurrencies unless currency == other.currency
+ amount == other.amount
+ end
+
+ def <=>(other)
+ raise ArgumentError unless other.kind_of?(Money)
+ raise IncompatibleCurrencies unless currency == other.currency
+ amount <=> other.amount
+ end
+end