Радослав обнови решението на 16.01.2013 16:14 (преди около 12 години)
+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ class Unknown < RuntimeError
+ def exception(message)
+ end
+ end
+
+ def initialize
+ @rates = {}
+ end
+
+ def set(from_c, to_c, rate)
+ if from_c != to_c
+ @rates[from_c] ||= {}
+ @rates[from_c][to_c] = rate
+ end
+ end
+
+ def get(from_c, to_c)
+ if from_c == to_c then BigDecimal.new('1')
+ elsif rate_exists(from_c, to_c) then BigDecimal.new(@rates[from_c][to_c])
+ elsif rate_exists(to_c, from_c) then BigDecimal.new(1/@rates[to_c][from_c])
+ end
+ end
+
+ def convert(from_c, to_c, amount)
+ begin
+ amount * get(from_c, to_c)
+ rescue
+ raise Unknown
+ end
+ end
+
+ private
+ def rate_exists(from_c, to_c)
+ @rates.has_key?(from_c) && @rates[from_c].has_key?(to_c)
+ end
+end
+
+class Money
+ include Comparable
+
+ attr_reader :currency, :amount
+
+ class IncompatibleCurrencies < StandardError
+ def exception
+ end
+ end
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ amount = @amount.round(2).to_digits.to_s
+ while not /[\d]{1,}\.\d\d/.match amount
+ amount << '0'
+ end
+ amount + ' ' + @currency.to_s
+ end
+
+ def in(currency, exchange_rate)
+ Money.new exchange_rate.convert(@currency, currency, amount), currency
+ end
+
+ def /(number)
+ if number.is_a? Numeric
+ Money.new @amount / number, @currency
+ else raise ArgumentError
+ end
+ end
+
+ def *(number)
+ if number.is_a? Numeric
+ Money.new @amount * number, @currency
+ else raise ArgumentError
+ end
+ end
+
+ def +(other)
+ if other.is_a? Money
+ if other.currency == @currency
+ Money.new @amount + other.amount, @currency
+ else raise IncompatibleCurrencies
+ end
+ else raise ArgumentError
+ end
+ end
+
+ def -(other)
+ if other.is_a? Money
+ if other.currency == @currency
+ Money.new @amount - other.amount, @currency
+ else raise IncompatibleCurrencies
+ end
+ else raise ArgumentError
+ end
+ end
+
+ def <=>(other)
+ if other.is_a? Money
+ if other.currency == @currency
+ @amount <=> other.amount
+ else raise IncompatibleCurrencies
+ end
+ else raise ArgumentError
+ end
+ end
+end