Class: String

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

Overview

Start monkey patch of Object::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

#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