Class: String

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

Instance Method Summary collapse

Instance Method Details

#camelizeObject



24
25
26
# File 'lib/core_ext.rb', line 24

def camelize
  self.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end

#pluralizeObject



16
17
18
19
20
21
22
# File 'lib/core_ext.rb', line 16

def pluralize
  case self
  when /y$/ then $` + "ies"
  when /s$/ then self + "es"
  else self + "s"
  end
end

#singularizeObject



8
9
10
11
12
13
14
# File 'lib/core_ext.rb', line 8

def singularize
  case self
  when /ies$/ then $` + "y"
  when /s$/ then $`
  else self.dup
  end
end

#underscoreObject



28
29
30
31
32
33
34
# File 'lib/core_ext.rb', line 28

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