Валентин обнови решението на 16.01.2013 04:54 (преди около 12 години)
+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ attr_reader :rates
+
+ class Rate
+ attr_reader :from_currency, :to_currency
+
+ def initialize(from_currency, to_currency, rate = nil)
+ @from_currency, @to_currency, @rate = from_currency, to_currency, rate
+ end
+
+ def get(to_currency)
+ if to_currency == @to_currency
+ @rate
+ else
+ 1/@rate
+ end
+ end
+
+ def hash
+ @from_currency.hash + @to_currency.hash
+ end
+
+ def eql?(other)
+ other.class == self.class &&
+ (@from_currency == other.from_currency && @to_currency == other.to_currency ||
+ @from_currency == other.to_currency && @to_currency == other.from_currency)
+ end
+ end
+
+ def initialize
+ @rates = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ # TODO Има ли начин да вземем стойността на ключа,
+ # за да не се налага да пазя нова инстанция на Rate като стойност в хеша?
+ @rates[Rate.new(from_currency, to_currency)] = Rate.new(from_currency, to_currency, rate)
+ end
+
+ def get(from_currency, to_currency)
+ @rates[Rate.new(from_currency, to_currency)] &&
+ @rates[Rate.new(from_currency, to_currency)].get(to_currency)
+ end
+
+ def convert(from_currency, to_currency, amount)
+ get(from_currency, to_currency) * amount
+ end
+end