Class: String

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

Instance Method Summary collapse

Instance Method Details

#camelize(first_letter_in_uppercase = true) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/ruby_ext/core/string.rb', line 33

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

#constantizeObject



41
42
43
44
# File 'lib/ruby_ext/core/string.rb', line 41

def constantize
  names = sub(/^::/, '').split '::'
  names.reduce(Object){|memo, name| memo.const_get name, false}
end

#dirnameObject



11
12
13
# File 'lib/ruby_ext/core/string.rb', line 11

def dirname
  File.expand_path(File.dirname(self))
end

#expand_pathObject



15
16
17
# File 'lib/ruby_ext/core/string.rb', line 15

def expand_path
  File.expand_path(self)
end

#indent(spaces = 2) ⇒ Object



2
3
4
5
# File 'lib/ruby_ext/core/string.rb', line 2

def indent spaces = 2
  indent = ' ' * spaces
  gsub /^/, indent
end

#rsplit(*args) ⇒ Object



7
8
9
# File 'lib/ruby_ext/core/string.rb', line 7

def rsplit *args
  reverse.split(*args).collect(&:reverse).reverse
end

#substitute(*args) ⇒ Object



46
47
48
# File 'lib/ruby_ext/core/string.rb', line 46

def substitute(*args)
  gsub(*args){yield Regexp.last_match.captures}
end

#substitute!(*args) ⇒ Object



50
51
52
# File 'lib/ruby_ext/core/string.rb', line 50

def substitute!(*args)
  gsub!(*args){yield Regexp.last_match.captures}
end

#to_aObject



19
20
21
# File 'lib/ruby_ext/core/string.rb', line 19

def to_a
  [self]
end

#underscoreObject



23
24
25
26
27
28
29
30
31
# File 'lib/ruby_ext/core/string.rb', line 23

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