Method: String#to_money

Defined in:
lib/more_money/core_extensions.rb

#to_money(currency = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/more_money/core_extensions.rb', line 25

def to_money(currency = nil)
  # Get the currency
  matches = scan(/([A-Z]{2,3})/)
  currency ||= matches[0] ? matches[0][0] : MoreMoney::Money.default_currency
  
  # Get the cents amount
  matches = scan(/(\-?\d+(\.(\d+))?)/)
  cents = matches[0] ? (matches[0][0].to_f * 100) : nil
  
  MoreMoney::Money.new(cents, currency)
end