Module: Archfiend::String::Underscore

Defined in:
lib/archfiend/core_ext/string/underscore.rb

Instance Method Summary collapse

Instance Method Details

#underscoreString

Returns String in the lowercase underscore format.

Returns:

  • (String)

    String in the lowercase underscore format



5
6
7
8
9
10
11
12
13
14
# File 'lib/archfiend/core_ext/string/underscore.rb', line 5

def underscore
  return self unless /[A-Z-]|::/.match?(self)

  word = gsub('::'.freeze, '/'.freeze)
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2'.freeze)
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2'.freeze)
  word.tr!('-'.freeze, '_'.freeze)
  word.downcase!
  word
end