Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/ruby-extensions/core/string.rb
Overview
Extension to the string class provided by Ruby that provides the following 2 methods:
-
String.pluralize
-
String.singularize
Note that these methods only work for English words, other languages such as Japanese or French aren’t supported at this time (January, 2011). This snippet also doesn’t cover every single english word, patches are welcome!
Constant Summary collapse
- SingularToPlural =
Constant containing all regular expressions for singular strings (that will be pluralized) and their replacements.
{ /ss$/ => 'sses', /se$/ => 'ses', /sh$/ => 'shes', /ge$/ => 'ges', /ch$/ => 'ches', /ge$/ => 'ges', /g$/ => 'gs', # When the singular form ends in a voiceless consonant (other than a sibilant). # # lap => laps # cat => cats # clock => clocks # cuff => cuffs # death => deaths /ap$/ => 'aps', /at$/ => 'ats', /ck$/ => 'cks', /ff$/ => 'ffs', /th$/ => 'ths', # Most nouns ending in o preceded by a consonant also form their plurals by adding -es # # hero => heroes # potato => potatoes # volcano => volcanoes or volcanos /o$/ => 'oes', # nouns ending in a y preceded by a consonant usually drop the y and add -ies # # cherry => cherries # lady => ladies # ywzxvtsrqpnmlkjhgfdcb # /([bcdfghjklmnpqrstvxzwy]+)y$/ => "\\1ies", # For all other words (i.e. words ending in vowels or voiced non-sibilants). # # boy => boys # girl => girls # chair => chairs # quiz => quizes /z$/ => 'zes', /y$/ => 'ys', /l$/ => 'ls', /r$/ => 'rs' }
- PluralToSingular =
Constant containing all regular expressions used to convert plural words to singular words.
{ /sses$/ => 'ss', /ses$/ => 'se', /shes$/ => 'sh', /ges$/ => 'ges', /ches$/ => 'ches', /ges$/ => 'ges', /aps$/ => 'ap', /ats$/ => 'at', /cks$/ => 'ck', /ffs$/ => 'ff', /ths$/ => 'th', /oes$/ => 'o', /ies$/ => 'y', /zes$/ => 'z', /ys$/ => 'y', /l$/ => 'ls', /r$/ => 'rs', /s$/ => '' }
Instance Method Summary collapse
-
#pluralize ⇒ String
Tries to convert a string to it’s pluralized version.
-
#pluralize! ⇒ Object
Converts the current string into a pluralized form and overwrites the old value rather than returning it.
-
#singularize ⇒ String
Tries to convert the current string into a singular version.
-
#singularize! ⇒ Object
Converts a plural string to it’s singular form and replaces the current value of the string with this singular version.
Instance Method Details
#pluralize ⇒ String
Tries to convert a string to it’s pluralized version. For example, “user” would result in “users” and “quiz” will result in “quizes”. This method will return the pluralized string, use pluralize! to overwrite the current (singular) version of the string with the pluralized one.
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/ruby-extensions/core/string.rb', line 112 def pluralize string = self.dup SingularToPlural.each do |regex, replace| new_string = string.gsub(regex, replace) if new_string != string return new_string end end return string end |
#pluralize! ⇒ Object
Converts the current string into a pluralized form and overwrites the old value rather than returning it.
139 140 141 |
# File 'lib/ruby-extensions/core/string.rb', line 139 def pluralize! self.replace(self.pluralize) end |
#singularize ⇒ String
Tries to convert the current string into a singular version.
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/ruby-extensions/core/string.rb', line 155 def singularize string = self.dup PluralToSingular.each do |regex, replace| new_string = string.gsub(regex, replace) if new_string != string return new_string end end return string end |
#singularize! ⇒ Object
Converts a plural string to it’s singular form and replaces the current value of the string with this singular version.
182 183 184 |
# File 'lib/ruby-extensions/core/string.rb', line 182 def singularize! self.replace(self.singularize) end |