Module: CoreX::String

Defined in:
lib/core_x/string.rb

Defined Under Namespace

Modules: MethodVersions

Class Method Summary collapse

Class Method Details

.camelcaseObject

Converts strings to UpperCamelCase. If the uppercase_first_letter parameter is set to false, then produces lowerCamelCase.

Also converts ‘/’ to ‘::’ which is useful for converting paths to namespaces.

camelize('active_model')                # => "ActiveModel"
camelize('active_model', false)         # => "activeModel"
camelize('active_model/errors')         # => "ActiveModel::Errors"
camelize('active_model/errors', false)  # => "activeModel::Errors"

As a rule of thumb you can think of camelize as the inverse of underscore.

(This is a mechanical method that doesn’t understand human stuff like acronyms.)



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/core_x/string.rb', line 38

def camelize(string, first_letter_small = false)
  string = string.to_s
  if !first_letter_small
    string = string.sub(/^[a-z\d]*/) { $&.capitalize }
  else
    string = string.sub(/^(?:(?=\b|[A-Z_])|\w)/) { $&.downcase }
  end
  string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
  string.gsub!(/\//, '::')
  string
end

.camelize(string, first_letter_small = false) ⇒ Object

Converts strings to UpperCamelCase. If the uppercase_first_letter parameter is set to false, then produces lowerCamelCase.

Also converts ‘/’ to ‘::’ which is useful for converting paths to namespaces.

camelize('active_model')                # => "ActiveModel"
camelize('active_model', false)         # => "activeModel"
camelize('active_model/errors')         # => "ActiveModel::Errors"
camelize('active_model/errors', false)  # => "activeModel::Errors"

As a rule of thumb you can think of camelize as the inverse of underscore.

(This is a mechanical method that doesn’t understand human stuff like acronyms.)



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/core_x/string.rb', line 26

def camelize(string, first_letter_small = false)
  string = string.to_s
  if !first_letter_small
    string = string.sub(/^[a-z\d]*/) { $&.capitalize }
  else
    string = string.sub(/^(?:(?=\b|[A-Z_])|\w)/) { $&.downcase }
  end
  string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
  string.gsub!(/\//, '::')
  string
end

.snakecaseObject

Makes an underscored, lowercase form from the expression in the string.

Changes ‘::’ to ‘/’ to convert namespaces to paths.

underscore('ActiveModel')         # => "active_model"
underscore('ActiveModel::Errors') # => "active_model/errors"

As a rule of thumb you can think of underscore as the inverse of #camelize

camelize(underscore('SSLError'))  # => "SslError"


62
63
64
65
66
67
68
69
70
# File 'lib/core_x/string.rb', line 62

def underscore(camel_cased_word)
  return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/
  word = camel_cased_word.to_s.gsub(/::/, '/')
  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

.underscore(camel_cased_word) ⇒ Object

Makes an underscored, lowercase form from the expression in the string.

Changes ‘::’ to ‘/’ to convert namespaces to paths.

underscore('ActiveModel')         # => "active_model"
underscore('ActiveModel::Errors') # => "active_model/errors"

As a rule of thumb you can think of underscore as the inverse of #camelize

camelize(underscore('SSLError'))  # => "SslError"


53
54
55
56
57
58
59
60
61
# File 'lib/core_x/string.rb', line 53

def underscore(camel_cased_word)
  return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/
  word = camel_cased_word.to_s.gsub(/::/, '/')
  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