module Evoke::Inflections::Underscore

String inflections for converting to underscored form.

Underscoring a string injects an underscore between CamelCase words, replaces all ‘::’ with ‘/’ and converts the string to lowercase. For example, the string “HelloWorld” is underscored to “hello_world”, and the string “Hello::World” is underscored to “hello/world”.

Public Instance Methods

underscore() click to toggle source

Creates an underscored, lowercase form of the string and changes ‘::’ to ‘/’ to convert namespaces to paths.

@example Underscoring “Example::HelloWorld”.

"Example::HelloWorld" # => "example/hello_world"

@return [String] The underscored string.

# File lib/evoke/inflections/underscore.rb, line 18
def underscore
  dup.tap do |s|
    s.gsub!(/::/, '/')
    s.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    s.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
    s.tr!('-', '_')
    s.downcase!
  end
end
underscore!() click to toggle source

Replaces the existing String instance with its underscored form.

@example Underscoring the string “Example::HelloWorld”.

string = "Example::HelloWorld"
string.underscore!
string # => "example/hello_world"

@return [String] This underscored form of the original string.

# File lib/evoke/inflections/underscore.rb, line 37
def underscore!
  replace(underscore)
end