Николай обнови решението на 11.01.2013 19:44 (преди около 12 години)
+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeType
+ attr_reader :from_currency, :to_currency
+
+ def initialize(from_currency, to_currency)
+ @from_currency = from_currency
+ @to_currency = to_currency
+ end
+
+ def reverse_exchange_type
+ ExchangeType.new(@to_currency, @from_currency)
+ end
+
+ def eql?(other)
+ @from_currency == other.from_currency and
+ @to_currency == other.to_currency
+ end
+
+ def hash
+ @from_currency.hash ^ @to_currency.hash
+ end
+end
+
+class ExchangeRate
+ def initialize
+ @exchange_rates = Hash.new
+ end
+
+ def set(from_currency, to_currency, rate)
+ exchange_type = ExchangeType.new(from_currency, to_currency)
+ reverse_exchange_type = exchange_type.reverse_exchange_type
+ rate_decimal = rate.to_d
+ @exchange_rates[exchange_type] = rate_decimal
+ @exchange_rates[reverse_exchange_type] = 1 / rate_decimal
+ end
+
+ def get(from_currency, to_currency)
+ if from_currency == to_currency
+ 1
+ else
+ exchange_type = ExchangeType.new(from_currency, to_currency)
+ @exchange_rates[exchange_type]
+ end
+ end
+
+ def convert(from_currency, to_currency, amount)
+ exchange_rate = get(from_currency, to_currency)
+ raise ExchangeRate::Unknown if exchange_rate.nil?
+ exchange_rate * amount
+ end
+
+ class Unknown < RuntimeError
+ end
+end
+
+class Money
+ attr_reader :amount, :currency
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ amount_string = '%.2f' % @amount
+ "#{amount_string} #{@currency.to_s}"
+ end
+
+ def in(currency, exchange_rate)
+ amount = exchange_rate.convert(@currency, currency, @amount)
+ Money.new(amount, currency)
+ end
+
+ [:*, :/].each do |operator|
+ define_method(operator) do |number|
+ amount = @amount.send(operator, number)
+ Money.new(amount, @currency)
+ end
+ end
+
+ [:+, :-, :<=>, :==, :<, :<=, :>, :>=].each do |operator|
+ define_method(operator) do |other|
+ ensure_correct_money_argument(other)
+ amount = @amount.send(operator, other.amount)
+ Money.new(amount, @currency)
+ end
+ end
+
+ def ensure_correct_money_argument(argument)
+ raise ArgumentError unless argument.is_a? Money
+ raise Money::IncompatibleCurrencies unless @currency == argument.currency
+ end
+
+ class IncompatibleCurrencies < StandardError
+ end
+end