Class: String

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

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/core_extensions/string.rb', line 38

def blank?
  dup.strip.length == 0 ? true : false
end

#is_plural?Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/core_extensions/string.rb', line 4

def is_plural?
  self.downcase.pluralize == self.downcase
end

#is_singular?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/core_extensions/string.rb', line 8

def is_singular?
  !self.is_plural?
end

#pluralizeObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/core_extensions/string.rb', line 27

def pluralize
  str = self.dup
  if str[-1] == 'y'
    str[0..-2] + 'ies'
  elsif str[-1] == 's'
    str
  else
    str + 's'
  end
end

#singularizeObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/core_extensions/string.rb', line 16

def singularize
  str = self.dup
  if str[-3..-1] == 'ies'
    str[0..-4] + 'y'
  elsif str[-1] == 's'
    str[0..-2]
  else
    str
  end
end

#titleizeObject



12
13
14
# File 'lib/core_extensions/string.rb', line 12

def titleize
  str = self[0].upcase + self[1..-1].downcase
end

#underscoreObject



42
43
44
45
46
47
48
# File 'lib/core_extensions/string.rb', line 42

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