Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/money/core_extensions.rb

Overview

Allows Writing of ‘100’.to_money for String types Excess characters will be discarded

'100'.to_money => #<Money @cents=10000>
'100.37'.to_money => #<Money @cents=10037>

Instance Method Summary collapse

Instance Method Details

#to_moneyObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/money/core_extensions.rb', line 15

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