Module: Evoke::Inflections::Camelize

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

Overview

String inflections for converting to CamelCase.

Camelizing a string takes all the compound words separated by an underscore and combines them together, capitalizing the first letter of each word. It also converts ‘/’ to ‘::’. For example “hello_world” is camelized to “HelloWorld”, and “hello/world” is camelized to “Hello::World”.

Instance Method Summary collapse

Instance Method Details

#camelizeString

Converts a string to CamelCase. It also converts ‘/’ to ‘::’.

Examples:

Camelize the string “example/hello_world”.


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

Returns:

  • (String)

    The CamelCase string.



17
18
19
20
21
22
23
# File 'lib/evoke/inflections/camelize.rb', line 17

def camelize
  dup.tap do |s|
    s.capitalize!
    s.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
    s.gsub!('/', '::')
  end
end

#camelize!String

Replaces the existing String instance with a CamelCase string.

Examples:

Camelizing the string “example/hello_world”.


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

Returns:

  • (String)

    This string modified to CamelCase.



34
35
36
# File 'lib/evoke/inflections/camelize.rb', line 34

def camelize!
  replace(camelize)
end