_4bdja
Last Updated: February 25, 2016
·
6.776K
· jeroenr

Pluralize without the number

It might seem quite obvious, but it took me some time to find this. If you use the pluralize helper, it will always include the number, rather than just pluralizing the word:

pluralize(2, 'day') 
# => 2 days

Apparently, Rails monkey-patched the String class to include a pluralize function which doesn't include the number:

"day".pluralize(2) 
# => days

I find it quite useful if you want to style the number differently from the text in your views.

Happy coding!

3 Responses
Add your response

3051

thanks for the tip!

over 1 year ago ·
8462

awesome easy!

over 1 year ago ·
11989

Awesome tip. I was just playing around with this and found that you don't need the number:

'day'.pluralize
=> 'days'

similarly:

'days'.singularize
=> 'day'

over 1 year ago ·