Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/chemlab/core_ext/string/inflections.rb,
lib/chemlab/core_ext/string/root_module.rb

Overview

Start monkey patch of String

Instance Method Summary collapse

Instance Method Details

#classifyObject

Classify a string

Examples:

'test_class'.classify #=> TestClass
'testclass'.classify #=> Testclass


10
11
12
# File 'lib/chemlab/core_ext/string/inflections.rb', line 10

def classify
  split('_').map(&:capitalize).join
end

#root_moduleModule

Find the root module (parent module) of a class or module

Examples:

'A::B::C'.root_module #=> A
'A::B'.root_module => A

Returns:

  • (Module)

    the root module



10
11
12
# File 'lib/chemlab/core_ext/string/root_module.rb', line 10

def root_module
  Object.const_get(split('::').first)
end

#underscoreObject

Underscore a multi-worded string

Examples:

'TestClass'.underscore #=> 'test_class'
'Class'.underscore #=> 'class'


19
20
21
22
23
24
# File 'lib/chemlab/core_ext/string/inflections.rb', line 19

def underscore
  chars.each_with_object(+'') do |c, str|
    str << '_' if c.match?(/[A-Z]/) && !str.size.zero?
    str << c.downcase
  end
end