Module: MoreCoreExtensions::StringToIWithMethod

Defined in:
lib/more_core_extensions/core_ext/string/to_i_with_method.rb

Constant Summary collapse

NUMBER_WITH_METHOD_REGEX =
/^([0-9\.,]+)\.([a-z]+)$/.freeze

Instance Method Summary collapse

Instance Method Details

#number_with_method?Boolean

Determines if the object contains a number with a method invocation

"20".number_with_method?           # => false
"20.percent".number_with_method?   # => true
"20.0.percent".number_with_method? # => true

Returns:

  • (Boolean)


59
60
61
# File 'lib/more_core_extensions/core_ext/string/to_i_with_method.rb', line 59

def number_with_method?
  self =~ NUMBER_WITH_METHOD_REGEX
end

#to_f_with_methodObject

Converts to a Float while also evaluating a method invocation

This method is similar to #to_f, but does not support extraneous characters.

"20".to_f_with_method           # => 20.0
"20.percent".to_f_with_method   # => 20.0
"20.megabytes".to_f_with_method # => 20_971_520.0

"20.1".to_f_with_method           # => 20.1
"20.1.percent".to_f_with_method   # => 20.1
"20.1.megabytes".to_f_with_method # => 21_076_377.6

20.to_f_with_method   # => 20.0
20.1.to_f_with_method # => 20.1
nil.to_f_with_method  # => 0.0


42
43
44
# File 'lib/more_core_extensions/core_ext/string/to_i_with_method.rb', line 42

def to_f_with_method
  to_x_with_method.to_f
end

#to_i_with_methodObject

Converts to an Integer while also evaluating a method invocation

This method is similar to #to_i, but does not support extraneous characters nor bases other than 10.

"20".to_i_with_method           # => 20
"20.percent".to_i_with_method   # => 20
"20.megabytes".to_i_with_method # => 20_971_520

"20.0".to_i_with_method           # => 20
"20.0.percent".to_i_with_method   # => 20
"20.0.megabytes".to_i_with_method # => 20_971_520

20.to_i_with_method   # => 20
20.0.to_i_with_method # => 20
nil.to_i_with_method  # => 0


22
23
24
# File 'lib/more_core_extensions/core_ext/string/to_i_with_method.rb', line 22

def to_i_with_method
  to_x_with_method.to_i
end