Module: Tins::StringUnderscore
- Defined in:
- lib/tins/string_underscore.rb
Overview
Tins::StringUnderscore provides methods for converting camelCase and PascalCase strings to snake_case format.
This module contains the ‘underscore` method which is commonly used in Ruby applications, particularly in Rails-style naming conventions where developers need to convert between different naming styles.
Instance Method Summary collapse
-
#underscore ⇒ String
Convert a camelCase or PascalCase string to snake_case format.
Instance Method Details
#underscore ⇒ String
Convert a camelCase or PascalCase string to snake_case format.
This method handles various naming conventions:
-
Converts camelCase to snake_case (e.g., “camelCase” → “camel_case”)
-
Converts PascalCase to snake_case (e.g., “PascalCase” → “pascal_case”)
-
Handles consecutive uppercase letters (e.g., “XMLParser” → “xml_parser”)
-
Replaces dashes with underscores
-
Converts to lowercase
-
Handles nested module paths with ‘::’ separators
36 37 38 39 40 41 42 43 44 |
# File 'lib/tins/string_underscore.rb', line 36 def underscore word = dup word.gsub!(/::/, '/') word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") word.downcase! word end |