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
25 26 27 |
# File 'lib/core_extensions/string/transformations.rb', line 25 def humanize gsub(/_id$/, '').tr('_', ' ').capitalize end |
#to_camel ⇒ String
Convert underscored_text to CamelCase
19 20 21 |
# File 'lib/core_extensions/string/transformations.rb', line 19 def to_camel split('_').map(&:capitalize).join end |
#to_underscore ⇒ String
Convert CamelCase to underscored_text
9 10 11 12 13 14 15 |
# 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 |