Thursday, April 17, 2008

Pluralize

Have you ever had to write some sort of if/else condition to display a sentence involving an object count? Something like 1 person, 2 people, etc.

Well, RoR has a nifty little method called pluralize. All you do is feed it the number and a string, and it will attempt to figure out exactly what you want.

For example:

pluralize(1, 'person') # '1 person'
pluralize(2, 'person') # '2 people'

I thought this was pretty neat and wanted to share with you guys. There are a lot of little methods tucked away in the RoR API, and I encourage you to look through them to help minimize your development time on mundane things like this. Enjoy!


W

No comments: