Module: Evoke::Inflections::Underscore

Defined in:
lib/evoke/inflections/underscore.rb

Overview

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”.

Instance Method Summary collapse

Instance Method Details

#underscoreString

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

Examples:

Underscoring “Example::HelloWorld”.


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

Returns:

  • (String)

    The underscored string.



18
19
20
21
22
23
24
25
26
# 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!String

Replaces the existing String instance with its underscored form.

Examples:

Underscoring the string “Example::HelloWorld”.


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

Returns:

  • (String)

    This underscored form of the original string.



37
38
39
# File 'lib/evoke/inflections/underscore.rb', line 37

def underscore!
  replace(underscore)
end