Module: Kutils::StringUtils

Defined in:
lib/utils/string_utils.rb

Overview

StringUtils provides string format conversion and cleaning utilities.

Class Method Summary collapse

Class Method Details

.camelize(str) ⇒ String

Convert snake_case to CamelCase

Examples:

Kutils::StringUtils.camelize('hello_world') #=> 'HelloWorld'

Parameters:

  • str (String)

Returns:

  • (String)


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)

Examples:

Kutils::StringUtils.slugify('Hello Ruby!') #=> 'hello-ruby'

Parameters:

  • str (String)

Returns:

  • (String)


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

Examples:

Kutils::StringUtils.underscore('HelloWorld') #=> 'hello_world'

Parameters:

  • str (String)

Returns:

  • (String)


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