Никола обнови решението на 16.01.2013 16:41 (преди около 12 години)
+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ def initialize
+ @rates = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ set_currency from_currency, to_currency, rate
+ set_currency to_currency, from_currency, '1'.to_d / rate
+ end
+
+ def get(from_currency, to_currency)
+ return '1'.to_d if from_currency == to_currency
+
+ @rates[from_currency].nil? ? nil : @rates[from_currency][to_currency]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ rate = get(from_currency, to_currency)
+
+ raise Unknown if rate.nil?
+
+ amount * rate
+ end
+
+ class Unknown < RuntimeError
+ end
+
+ private
+
+ def set_currency(from_currency, to_currency, rate)
+ @rates[from_currency] ||= {}
+ @rates[from_currency][to_currency] = rate
+ end
+end
+
+class Money
+ include Comparable
+
+ attr_reader :amount, :currency
+
+ def initialize(amount, currency)
+ @amount, @currency = amount, currency
+ end
+
+ def in(currency, exchange_rate)
+ Money.new exchange_rate.convert(@currency, currency, @amount), currency
+ end
+
+ def *(coefficient)
+ validate_numeric coefficient
+
+ Money.new @amount * coefficient, @currency
+ end
+
+ def /(coefficient)
+ validate_numeric coefficient
+
+ Money.new @amount / coefficient, @currency
+ end
+
+
+ def +(money)
+ validate_money money
+
+ Money.new @amount + money.amount, @currency
+ end
+
+ def -(money)
+ validate_money money
+
+ Money.new @amount - money.amount, @currency
+ end
+
+ def <=>(money)
+ validate_money money
+
+ @amount <=> money.amount
+ end
+
+ def to_s
+ "%.2f #{@currency}" % amount
+ end
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ private
+
+ def validate_numeric(coefficient)
+ raise ArgumentError if not coefficient.is_a? Numeric
+ end
+
+ def validate_money(money)
+ raise ArgumentError if not money.is_a? Money
+ raise IncompatibleCurrencies if @currency != money.currency
+ end
+end