Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/core_extensions/string.rb
Instance Method Summary collapse
- #blank? ⇒ Boolean
- #is_plural? ⇒ Boolean
- #is_singular? ⇒ Boolean
- #pluralize ⇒ Object
- #singularize ⇒ Object
- #titleize ⇒ Object
- #underscore ⇒ Object
Instance Method Details
#blank? ⇒ Boolean
38 39 40 |
# File 'lib/core_extensions/string.rb', line 38 def blank? dup.strip.length == 0 ? true : false end |
#is_plural? ⇒ Boolean
4 5 6 |
# File 'lib/core_extensions/string.rb', line 4 def is_plural? self.downcase.pluralize == self.downcase end |
#is_singular? ⇒ Boolean
8 9 10 |
# File 'lib/core_extensions/string.rb', line 8 def is_singular? !self.is_plural? end |
#pluralize ⇒ Object
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 |
#singularize ⇒ Object
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 |
#titleize ⇒ Object
12 13 14 |
# File 'lib/core_extensions/string.rb', line 12 def titleize str = self[0].upcase + self[1..-1].downcase end |
#underscore ⇒ Object
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 |