Class: String

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

Instance Method Summary collapse

Instance Method Details

#camelize(uppercase_first_letter = true) ⇒ Object

Barrow these from ActiveSupport



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

def camelize(uppercase_first_letter = true)
  string = self.dup
  if uppercase_first_letter
    string = string.sub(/^[a-z\d]*/) { $&.capitalize }
  end
  string.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }.gsub('/', '::')
end

#constantizeObject



23
24
25
# File 'lib/ext/string.rb', line 23

def constantize
  Object.const_get(self)
end

#from_jsonObject

Custom additions



28
29
30
# File 'lib/ext/string.rb', line 28

def from_json
  JSON.parse(self)
end

#underscoreObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/ext/string.rb', line 12

def underscore
  word = self.dup
  word.gsub!('::', '/')
  word.gsub!(/(?:([A-Za-z\d])|^)((?=a)b)(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" }
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end