Method: PulseMeter::Mixins::Utils#underscore

Defined in:
lib/pulse_meter/mixins/utils.rb

#underscore(str) ⇒ String

Converts string from CamelCase to snake_case

Parameters:

  • str (String)

    string to be underscore

Returns:

  • (String)

Raises:

  • (ArgumentError)

    unless passed value responds to to_s



95
96
97
98
99
100
101
102
# File 'lib/pulse_meter/mixins/utils.rb', line 95

def underscore(str)
  raise ArgumentError unless str.respond_to?(:to_s)
  str.to_s.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end