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_money(currency = nil) ⇒ Object



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

def to_money(currency = nil)
  if Money.config.legacy_deprecations
    Money::Parser::Fuzzy.parse(self, currency).tap do |money|
      message = "`#{self}.to_money` will behave like `Money.new` and raise on the next release. " \
        "To parse user input, do so on the browser and use the user's locale."
      Money.deprecate(message) if money.value != BigDecimal(self, exception: false)
    end
  else
    Money.new(self, currency)
  end
end