Is there a pluralize function in Ruby NOT Rails?

aarona picture aarona · Mar 15, 2010 · Viewed 30.4k times · Source

I am writing some Ruby code, not Rails, and I need to handle something like this:

found 1 match
found 2 matches

I have Rails installed so maybe I might be able to add a require clause at the top of the script, but does anyone know of a RUBY method that pluralizes strings? Is there a class I can require that can deal with this if the script isn't Rails but I have Rails installed?

Edit: All of these answers were close but I checked off the one that got it working for me. Try this method as a helper when writing Ruby, not Rails, code:

def pluralize(number, text)
  return text.pluralize if number != 1
  text
end

Answer

scaney picture scaney · Mar 16, 2010

Actually all you need to do is

require 'active_support/inflector'

and that will extend the String type.

you can then do

"MyString".pluralize

which will return

"MyStrings"

for 2.3.5 try:

require 'rubygems'
require 'active_support/inflector'

should get it, if not try

sudo gem install activesupport

and then the requires.