Class: Drillbit::Utilities::String

Inherits:
Object
  • Object
show all
Defined in:
lib/drillbit/utilities/string.rb

Class Method Summary collapse

Class Method Details

.underscore(other) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/drillbit/utilities/string.rb', line 5

def self.underscore(other)
  word = other.to_s.gsub('::', '/')
  word.gsub!(/(?:([A-Za-z\d])|^)(?=\b|[^a-z])/) do
    "#{Regexp.last_match(1)}#{Regexp.last_match(1) && ''}"
  end
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!('-', '_')
  word.downcase!
  word
end