Александър обнови решението на 16.01.2013 15:01 (преди около 12 години)
+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ class Unknown < RuntimeError
+ end
+
+ def initialize
+ @rates = Hash.new do |hash, (from_currency, to_currency)|
+ from_currency == to_currency ? 1.to_d : nil
+ end
+ end
+
+ def set(from_currency, to_currency, rate)
+ return if from_currency == to_currency
+ @rates[[from_currency, to_currency]] = rate
+ @rates[[to_currency, from_currency]] = 1 / rate
+ end
+
+ def get(from_currency, to_currency)
+ @rates[[from_currency, to_currency]]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ rate = @rates[[from_currency, to_currency]]
+ if rate
+ @rates[[from_currency, to_currency]] * amount
+ else
+ raise Unknown.new("rate between #{from_currency} and #{to_currency} is unknown")
+ end
+ end
+end
+
+
+class Money
+ include Comparable
+ attr_accessor :amount, :currency
+
+ class IncompatibleCurrencies < StandardError
+ end
+
+ def initialize(amount, currency)
+ @amount, @currency = amount, currency
+ end
+
+ def to_s
+ "#{'%.2f' % @amount} #{@currency}"
+ end
+
+ def in(currency, exchange_rate)
+ Money.new(exchange_rate.convert(@currency, currency, @amount), currency)
+ end
+
+ def *(other)
+ raise ArgumentError unless Numeric === other
+ Money.new(@amount * other, currency)
+ end
+
+ def /(other)
+ raise ArgumentError if not Numeric === other
+ Money.new(@amount / other, currency)
+ end
+
+ def +(other)
+ raise ArgumentError if not Money === other
+ if @currency != other.currency
+ raise IncompatibleCurrencies.new("#@currency is not compatible with #{other.currency}")
+ else
+ Money.new(@amount + other.amount, @currency)
+ end
+ end
+
+ def -(other)
+ raise ArgumentError if not Money === other
+ if @currency != other.currency
+ raise IncompatibleCurrencies.new("#@currency is not compatible with #{other.currency}")
+ else
+ Money.new(@amount - other.amount, @currency)
+ end
+ end
+
+ def <=>(other)
+ if Money === other and @currency != other.currency
+ raise IncompatibleCurrencies.new("#@currency is not compatible with #{other.currency}")
+ elsif Money === other
+ @amount <=> other.amount
+ else
+ raise ArgumentError.new("expected Money got #{other.class}")
+ end
+ end
+end