Йордан обнови решението на 16.01.2013 14:08 (преди около 12 години)
+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class Exchange
+ attr_reader :from_currency, :to_currency
+ attr_accessor :rate
+
+ def initialize(from_currency, to_currency, rate)
+ @from_currency = from_currency
+ @to_currency = to_currency
+ @rate = rate
+ end
+end
+
+class ExchangeRate
+ class Unknown < RuntimeError
+ end
+
+ def initialize
+ @exchanges = []
+ end
+
+ def set(from_currency, to_currency, rate)
+ if find_index from_currency, to_currency
+ @exchanges[find_index from_currency, to_currency].rate = rate
+ @exchanges[find_index to_currency, from_currency].rate = BigDecimal.new(1/rate)
+ else
+ @exchanges << Exchange.new(from_currency, to_currency, rate)
+ @exchanges << Exchange.new(to_currency, from_currency, BigDecimal.new(1/rate))
+ end
+ end
+
+ def get(from_currency, to_currency)
+ @exchanges[find_index from_currency, to_currency].rate if find_index from_currency, to_currency
+ end
+
+ def convert(from_currency, to_currency, amount)
+ if from_currency == to_currency then amount
+ elsif not find_index from_currency, to_currency then raise Unknown
+ else BigDecimal.new(@exchanges[find_index from_currency, to_currency].rate * amount)
+ end
+ end
+
+ private
+
+ def find_index(from_currency, to_currency)
+ @exchanges.index do |exchange|
+ exchange.from_currency == from_currency and
+ exchange.to_currency == to_currency
+ end
+ end
+end
+
+class Money
+ include Comparable
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ attr_reader :amount, :currency
+
+ def initialize(amount, currency)
+ @amount = amount.round(2)
+ @currency = currency
+ end
+
+ def to_s
+ if @amount.to_s("F").match /\.\d\d\z/ then @amount.to_s("F") + " " + @currency.to_s
+ else @amount.to_s("F") + "0 " + @currency.to_s
+ end
+ end
+
+ def in(currency, exchange_rate)
+ Money.new exchange_rate.convert(@currency, currency, @amount), currency
+ end
+
+ def *(number)
+ raise ArgumentError if not number.is_a? Numeric
+ Money.new (self.amount * number).to_d, @currency
+ end
+
+ def /(number)
+ raise ArgumentError if not number.is_a? Numeric
+ Money.new (self.amount / number).to_d, @currency
+ end
+
+ def +(other)
+ raise ArgumentError if not other.is_a? Money
+ raise IncompatibleCurrencies if self.currency != other.currency
+ Money.new (self.amount + other.amount).to_d, @currency
+ end
+
+ def -(other)
+ raise ArgumentError if not other.is_a? Money
+ raise IncompatibleCurrencies if self.currency != other.currency
+ Money.new (self.amount - other.amount).to_d, @currency
+ end
+
+ def <=>(other)
+ raise ArgumentError if not other.is_a? Money
+ raise IncompatibleCurrencies if self.currency != other.currency
+ self.amount <=> other.amount
+ end
+
+ def inspect
+ to_s
+ end
+end