module CoreExtensions::String::Transformations

Monkey-patches for String to add some simple missing transformations

Public Instance Methods

humanize() click to toggle source

Attempt to guess a more human-like view of a string @return [String]

# File lib/core_extensions/string/transformations.rb, line 25
def humanize
  gsub(/_id$/, '').tr('_', ' ').capitalize
end
to_camel() click to toggle source

Convert underscored_text to CamelCase @return [String]

# File lib/core_extensions/string/transformations.rb, line 19
def to_camel
  split('_').map(&:capitalize).join
end
to_underscore() click to toggle source

Convert CamelCase to underscored_text @return [String]

# File lib/core_extensions/string/transformations.rb, line 9
def to_underscore
  gsub(/::/, '/')
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr('-', '_')
    .downcase
end