Returns the plural form of the word in the string.Usage
Examples
- "post".pluralize #=> "posts"
- "octopus".pluralize #=> "octopi"
- "sheep".pluralize #=> "sheep"
- "words".pluralize #=> "words"
- "the blue mailman".pluralize #=> "the blue mailmen"
- "CamelOctopus".pluralize #=> "CamelOctopi"
I generally use pluralize for creating nice messages for users. The pluralize method can also be helpful for metaprogramming when converting from a class name to a table name.
Test
unit_tests do
test "change singular word to plural word" do
assert_equal "names", "name".pluralize
end
end
"CamelOctopus".pluralize #=> "CamelOctopi"
ReplyDeleteUgh ... it frustrates me that Rails codifies a grammatical error.
The plural of "octopus" is "octopodes" -- or "octopuses" if you wish to Anglicize it -- not "octopi".
Even though the word ends in -us, "pus" is a Greek root and not a Latin one, meaning "foot" -- witness "pedestrian" or "pedal".
And it also chokes on another common construct:
ReplyDelete"attorney general".pluralize #=> "attorney generals"
It's actually "attorneys general".
That one is more easily understood -- it would be non-trivial to identify "attorney general" as a noun-adjective construct.
But actually specifying something incorrect, like "octopi"? Blech.
Not a problem, Joe. active_support lets one customize pluralization rules.
ReplyDeleteInflector.inflections do |inflect|
inflect.irregular 'octopus', 'octopodes'
inflect.irregular 'attorney general', 'attorneys general'
end
Brian:
ReplyDeleteYeah, but the problem is that RoR ships with an error. The octopus/octopi thing appears six times in lib/active_support/inflector.rb as an example of the "right" way to do pluralization/singularization. :(
A nitpick, I know.
Hi Jay, I think there's a copy/paste bug in the name of the test.
ReplyDelete-Alistair
Thanks Alistair, it should be fixed now.
ReplyDeletehttp://www.reference.com/search?r=13&q=Octopodes
ReplyDeleteaccording to reference.com 3 forms are right : octopus = 'eight-legs', with plural forms: octopuses, octopi, or octopodes and reference.com uses octopuses as plural form in text.
just noticed...
What's the problem? Rails is open source. Stop whining, fix it, and submit a pull request.
ReplyDelete