Module: CoreExtensions::String::Transformations
- Defined in:
- lib/core_extensions/string/transformations.rb
Overview
Monkey-patches for String to add some simple missing transformations
Instance Method Summary collapse
-
#humanize ⇒ String
Attempt to guess a more human-like view of a string.
-
#to_camel ⇒ String
Convert underscored_text to CamelCase.
-
#to_underscore ⇒ String
Convert CamelCase to underscored_text.
Instance Method Details
#humanize ⇒ String
Attempt to guess a more human-like view of a string
23 24 25 |
# File 'lib/core_extensions/string/transformations.rb', line 23 def humanize gsub(/_id$/, '').tr('_', ' ').capitalize end |
#to_camel ⇒ String
Convert underscored_text to CamelCase
17 18 19 |
# File 'lib/core_extensions/string/transformations.rb', line 17 def to_camel split('_').map(&:capitalize).join end |
#to_underscore ⇒ String
Convert CamelCase to underscored_text
7 8 9 10 11 12 13 |
# File 'lib/core_extensions/string/transformations.rb', line 7 def to_underscore gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr('-', '_') .downcase end |