Module: Kutils::StringUtils
- Defined in:
- lib/utils/string_utils.rb
Overview
StringUtils provides string format conversion and cleaning utilities.
Class Method Summary collapse
-
.camelize(str) ⇒ String
Convert snake_case to CamelCase.
-
.slugify(str) ⇒ String
Convert string to URL slug (lowercase, dash-separated, alphanumeric).
-
.underscore(str) ⇒ String
Convert CamelCase or kebab-case to snake_case.
Class Method Details
.camelize(str) ⇒ String
Convert snake_case to CamelCase
15 16 17 |
# File 'lib/utils/string_utils.rb', line 15 def self.camelize(str) str.split('_').map(&:capitalize).join end |
.slugify(str) ⇒ String
Convert string to URL slug (lowercase, dash-separated, alphanumeric)
33 34 35 |
# File 'lib/utils/string_utils.rb', line 33 def self.slugify(str) str.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '') end |
.underscore(str) ⇒ String
Convert CamelCase or kebab-case to snake_case
24 25 26 |
# File 'lib/utils/string_utils.rb', line 24 def self.underscore(str) str.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\\1_\\2').gsub(/([a-z\d])([A-Z])/,'\\1_\\2').tr("-", "_").downcase end |