Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

pluralize, depending on count (See related posts)

// from Agile WebDevelopment with rails
// slightly extendet

def pluralize(count, noun, text)
  case count
  when 0: "There are no #{noun.pluralize} #{text}"
  when 1: "There is one #{noun} #{text}"
  else: "There are #{count} #{noun.pluralize} #{text}"
  end
end

Comments on this post

webwesen posts on Dec 23, 2006 at 02:38
there is no need for colon in
else "There are #{count} #{noun.pluralize} #{text}"
fermion posts on Feb 12, 2007 at 18:32
I believe that there's a helper for this now, something like:

pluralize( @object, 'singular', 'multiple')

where 'singular' and 'multiple' are the variants of the word for a single version and multiple one. If @object is an array, this just does the right thing.

You need to create an account or log in to post comments to this site.


Related Posts