Георги обнови решението на 15.01.2013 01:10 (преди около 12 години)
+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ def initialize
+ @rates = {}
+ end
+
+ def convert(currency_1, currency_2, amount)
+ rate = get(currency_1, currency_2)
+ raise ExchangeRate::Unknown if rate == nil
+ rate * amount
+ end
+
+ def set(currency_1, currency_2, rate)
+ @rates[to_hash_key(currency_1, currency_2)] = rate
+ @rates[to_hash_key(currency_2, currency_1)] = 1/rate
+ end
+
+ def get(currency_1, currency_2)
+ return 1 if currency_1 == currency_2
+ @rates[to_hash_key(currency_1, currency_2)]
+ end
+
+ def to_hash_key (currency_1, currency_2)
+ currency_1.to_s + currency_2.to_s
+ end
+
+ def exception(message)
+ Unknown.new message
+ end
+
+ class Unknown < RuntimeError
+ end
+
+end
+
+
+class Money
+ include Comparable
+ attr_accessor :amount, :currency
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ format("%.2f", @amount) + " " + @currency.to_s
+ end
+
+ def in(currency,exchange_rate)
+ Money.new(exchange_rate.convert(@currency,currency, @amount), currency)
+ end
+
+ def *(numeric)
+ Money.new(@amount*numeric, @currency)
+ end
+
+ def /(numeric)
+ Money.new(@amount/numeric, @currency)
+ end
+
+ def +(other)
+ check_argument(other)
+ raise Money::IncompatibleCurrencies unless other.currency == @currency
+ Money.new(@amount + other.amount, @currency)
+ end
+
+ def -(other)
+ check_argument(other)
+ raise Money::IncompatibleCurrencies unless other.currency == @currency
+ Money.new(@amount - other.amount, @currency)
+ end
+
+ def <=>(other)
+ check_argument(other)
+ raise Money::IncompatibleCurrencies unless other.currency == @currency
+ (self-other).amount
+ end
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ def check_argument(money)
+ raise ArgumentError unless money.kind_of? Money
+ end
+
+end
+
+