Module: Tins::StringCamelize
- Defined in:
- lib/tins/string_camelize.rb
Overview
Tins::StringCamelize provides methods for converting snake_case strings to camelCase or PascalCase format.
This module contains the ‘camelize` 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
-
#camelize(first_letter = :upper) ⇒ String
(also: #camelcase)
Convert a snake_case string to camelCase or PascalCase format.
Instance Method Details
#camelize(first_letter = :upper) ⇒ String Also known as: camelcase
Convert a snake_case string to camelCase or PascalCase format.
This method handles various naming conventions:
-
Converts snake_case to PascalCase (default) or camelCase
-
Handles nested module paths with ‘/’ separators
-
Supports different first letter cases
34 35 36 37 38 39 40 41 |
# File 'lib/tins/string_camelize.rb', line 34 def camelize(first_letter = :upper) case first_letter when :upper, true gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } when :lower, false self[0].chr.downcase + camelize[1..-1] end end |