Last Updated: September 09, 2019
·
7.832K
· Jeroen Rosenberg

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

thanks for the tip!

over 1 year ago ·

awesome easy!

over 1 year ago ·

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 ·