Class: Virtuous::StringHelper
- Inherits:
-
Object
- Object
- Virtuous::StringHelper
- Defined in:
- lib/virtuous/helpers/string_helper.rb
Overview
Helper methods for strings
Class Method Summary collapse
-
.camelize(string) ⇒ String
Transform a String from
snake_case
toCamelCase
. -
.underscore(string) ⇒ String
Transform a String from
CamelCase
tosnake_case
.
Class Method Details
.camelize(string) ⇒ String
Transform a String from snake_case
to CamelCase
.
22 23 24 25 26 27 28 |
# File 'lib/virtuous/helpers/string_helper.rb', line 22 def camelize(string) string = string.sub(/^(?:(?=\b|[A-Z_])|\w)/, &:downcase) string.gsub(%r{(?:_|(/))([a-z\d]*)}) do "#{::Regexp.last_match(1)}#{::Regexp.last_match(2).capitalize}" end.gsub('/', '::') end |
.underscore(string) ⇒ String
Transform a String from CamelCase
to snake_case
.
10 11 12 13 14 15 16 |
# File 'lib/virtuous/helpers/string_helper.rb', line 10 def underscore(string) string.gsub('::', '/') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr('-', '_') .downcase end |