Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/ext/string.rb
Overview
Core string extensions
Instance Method Summary collapse
-
#constantize ⇒ Object
Look up a constant via a string.
-
#underscore ⇒ Object
(also: #snakecase)
Underscore a String.
Instance Method Details
#constantize ⇒ Object
Look up a constant via a string.
rubocop:disable LineLength
22 23 24 25 26 27 28 29 30 |
# File 'lib/ext/string.rb', line 22 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
9 10 11 12 13 14 15 |
# File 'lib/ext/string.rb', line 9 def underscore gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr('-', '_') .downcase end |