Word with an 'S' if quantity > 1

G

Guillaume Loader

Hello everyone!

Is there a method to add an 'S' to a word if there are more than 1 ?

For example : 1 day / 2 days ...

Or do I need to create an if statement?

Thank you!
 
S

snex

Hello everyone!

Is there a method to add an 'S' to a word if there are more than 1 ?

For example : 1 day / 2 days ...

Or do I need to create an if statement?

Thank you!

try String#pluralize from the active_support gem
 
R

Rob Biedenharn

try String#pluralize from the active_support gem


Or roll your own if you have a simple need:

3.times do |n|
puts "call me in #{n} day#{'s' unless n==1}"
end
call me in 0 days
call me in 1 day
call me in 2 days
=> 3

But if you don't know the noun that you'll be counting ('day'), then
the Inflector from ActiveSupport is the way to go. (snex's
recommendation of String#pluralize uses the Inflector internally).


-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top