Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/ext/string.rb
Overview
Core string extensions
Instance Method Summary collapse
-
#camelize ⇒ Object
(also: #classify)
Camelize a string.
-
#constantize ⇒ Object
Look up a constant via a string.
-
#underscore ⇒ Object
(also: #snakecase)
Underscore a String.
Instance Method Details
#camelize ⇒ Object Also known as: classify
Camelize a string
9 10 11 |
# File 'lib/ext/string.rb', line 9 def camelize split('_').collect(&:capitalize).join end |
#constantize ⇒ Object
Look up a constant via a string.
rubocop:disable LineLength
31 32 33 34 35 36 37 38 39 |
# File 'lib/ext/string.rb', line 31 def constantize names = split('::') names.shift if names.empty? || names.first.empty? constant = Object names.each do |name| constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name) end constant end |
#underscore ⇒ Object Also known as: snakecase
Underscore a String. This method will also downcase the string
18 19 20 21 22 23 24 |
# File 'lib/ext/string.rb', line 18 def underscore gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr('-', '_') .downcase end |