Теодор обнови решението на 16.01.2013 04:07 (преди около 12 години)
+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ class CurrencyPair < Struct.new(:from, :to)
+ end
+
+ class Unknown < RuntimeError
+ end
+
+ def initialize
+ @rates = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ return if from_currency == to_currency
+ @rates[CurrencyPair.new(from_currency, to_currency)] = rate
+ @rates[CurrencyPair.new(to_currency, from_currency)] = BigDecimal.new(1) / rate
+ end
+
+ def get(from_currency, to_currency)
+ @rates[CurrencyPair.new(from_currency, to_currency)]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ return amount if from_currency == to_currency
+ raise Unknown, 'Unknown' unless @rates.has_key? CurrencyPair.new(from_currency, to_currency)
+ amount * @rates[CurrencyPair.new(from_currency, to_currency)]
+ end
+end
+
+class Money
+ attr_reader :amount, :currency
+
+ include Comparable
+
+ class IncompatibleCurrencies < StandardError
+ end
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def in(currency, exchange_rate)
+ Money.new(exchange_rate.convert(@currency, currency, @amount), currency)
+ end
+
+ def +(other)
+ raise ArgumentError, 'Incompatible types' unless other.instance_of? Money
+ raise IncompatibleCurrencies, 'Currencies not matching' unless self.currency == other.currency
+ Money.new (self.amount + other.amount), currency
+ end
+
+ def -(other)
+ raise ArgumentError, 'Incompatible types' unless other.instance_of? Money
+ raise IncompatibleCurrencies, 'Currencies not matching' unless self.currency == other.currency
+ Money.new (self.amount - other.amount), currency
+ end
+
+ def *(other)
+ raise ArgumentError, 'Incompatible types' unless other.is_a? Numeric
+ Money.new (@amount * other), currency
+ end
+
+ def /(other)
+ raise ArgumentError, 'Incompatible types' unless other.is_a? Numeric
+ Money.new (@amount / other), currency
+ end
+
+ def <=>(other)
+ raise ArgumentError, 'Bad type' unless other.instance_of? Money
+ raise IncompatibleCurrencies, 'Currencies not matching' unless self.currency == other.currency
+ self.amount <=> other.amount
+ end
+
+ def to_s
+ "%.2f #@currency" % @amount.truncate(2).to_s('F')
+ end
+end