Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ext/string.rb

Instance Method Summary collapse

Instance Method Details

#camelize(first_letter_in_uppercase = true) ⇒ Object

Adapted from camelize from ActiveSupport::Inflector



18
19
20
21
22
23
24
# File 'lib/ext/string.rb', line 18

def camelize first_letter_in_uppercase = true
  if first_letter_in_uppercase
    self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  else
    self.to_s[0].chr.downcase + self[1..-1].camelize
  end
end

#constantizeObject

Based on constantize from ActiveSupport::Inflector



4
5
6
7
8
9
10
11
12
13
# File 'lib/ext/string.rb', line 4

def constantize
  names = self.split('::')
  names.shift if names.empty? || names.first.empty?

  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

#underscoreObject

Adapted from underscore from ActiveSupport::Inflector



29
30
31
32
33
34
35
36
37
# File 'lib/ext/string.rb', line 29

def underscore
  word = self.dup
  word.gsub!(/::/, '/')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end