Class: String

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

Overview

Open String to add new methods.

Instance Method Summary collapse

Instance Method Details

#to_currencyMoney::Currency

Converts the current string into a Currency object.

Examples:

"USD".to_currency #=> #<Money::Currency id: usd>

Returns:

Raises:



59
60
61
# File 'lib/money/core_extensions.rb', line 59

def to_currency
  Money::Currency.new(self)
end

#to_money(currency = nil) ⇒ Money

Parses the current string and converts it to a Money object. Excess characters will be discarded.

Examples:

'100'.to_money                #=> #<Money @cents=10000>
'100.37'.to_money             #=> #<Money @cents=10037>
'100 USD'.to_money            #=> #<Money @cents=10000, @currency=#<Money::Currency id: usd>>
'USD 100'.to_money            #=> #<Money @cents=10000, @currency=#<Money::Currency id: usd>>
'$100 USD'.to_money           #=> #<Money @cents=10000, @currency=#<Money::Currency id: usd>>
'hello 2000 world'.to_money   #=> #<Money @cents=200000 @currency=#<Money::Currency id: usd>>

Parameters:

  • currency (Currency, String, Symbol) (defaults to: nil)

    The currency to set the resulting Money object to.

Returns:

See Also:



45
46
47
# File 'lib/money/core_extensions.rb', line 45

def to_money(currency = nil)
  Money.parse(self, currency)
end