Class: Google::StringUtils
- Inherits:
-
Object
- Object
- Google::StringUtils
- Defined in:
- lib/google/string_utils.rb
Overview
Helper class to process and mutate strings.
Class Method Summary collapse
-
.camelize(source, style = :lower) ⇒ Object
Converts string from underscore to camel case.
-
.underscore(source) ⇒ Object
Converts string from camel case to underscore.
Class Method Details
.camelize(source, style = :lower) ⇒ Object
Converts string from underscore to camel case
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/google/string_utils.rb', line 18 def self.camelize(source, style = :lower) camelized = source.gsub(/_(.)/, &:upcase).delete('_') case style when :lower camelized[0] = camelized[0].downcase when :upper camelized[0] = camelized[0].upcase else raise "Unknown camel case style: #{style}" end camelized end |
.underscore(source) ⇒ Object
Converts string from camel case to underscore
32 33 34 35 36 37 38 39 |
# File 'lib/google/string_utils.rb', line 32 def self.underscore(source) source.gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr('-', '_') .tr('.', '_') .downcase end |