Изабела обнови решението на 16.01.2013 15:55 (преди почти 12 години)
+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ attr_reader :rates
+
+ def initialize
+ @rates = Hash.new
+ end
+
+ def set(from_currency, to_currency, rate)
+ currencies = from_currency, to_currency
+ @rates[currencies] = rate
+ @rates[currencies.reverse] = 1 / rate
+ end
+
+ def get(from_currency, to_currency)
+ currencies = from_currency, to_currency
+ if from_currency == to_currency
+ '1'.to_d
+ else
+ @rates[currencies]
+ end
+ end
+
+ class Unknown < RuntimeError
+ end
+
+ def convert(from_currency, to_currency, amount)
+ rate = get(from_currency, to_currency)
+ raise Unknown.new("Not defined rate between #{from_currency} and #{to_currency}") unless rate
+ amount * rate
+ end
+end
+
+class Money
+ include Comparable
+
+ attr_reader :amount, :currency
+
+ def initialize(amount, currency)
+ @amount, @currency = amount, currency
+ end
+
+ def to_s
+ "#{sprintf "%.02f", @amount} #{@currency}"
+ end
+
+ def in(currency, exchange_rate)
+ converted_amount = exchange_rate.convert @currency, currency, @amount
+ Money.new converted_amount, currency
+ end
+
+ def *(multiplier)
+ raise ArgumentError.new("Wrong type of arguments") unless multiplier.kind_of? Numeric
+ Money.new(@amount * multiplier, @currency)
+ end
+
+ def /(divisor)
+ raise ArgumentError.new("Wrong type of arguments") unless divisor.kind_of? Numeric
+ Money.new(@amount / divisor, @currency)
+ end
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ def +(other)
+ raise ArgumentError.new("Wrong type of arguments") unless other.kind_of? Money
+ raise IncompatibleCurrencies.new("Currencies aren't equal") unless @currency == other.currency
+ Money.new(@amount + other.amount, @currency)
+ end
+
+ def -(other)
+ raise ArgumentError.new("Wrong type of arguments") unless other.kind_of? Money
+ raise IncompatibleCurrencies.new("Currencies aren't equal") unless @currency == other.currency
+ Money.new(@amount - other.amount, @currency)
+ end
+
+ def <=>(other)
+ raise ArgumentError.new("Wrong type of arguments") unless other.kind_of? Money
+ raise IncompatibleCurrencies.new("Currencies aren't equal") unless @currency == other.currency
+ @amount <=> other.amount
+ end
+end