Forum: Ruby Word with an 'S' if quantity > 1

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
007bac1f27905cd4d5d88315e9b0093a?d=identicon&s=25 Guillaume Loader (loader)
on 2009-02-18 20:29
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!
D0ac5c9608854166a5483061a169405e?d=identicon&s=25 snex (Guest)
on 2009-02-18 20:40
(Received via mailing list)
On Feb 18, 1:29 pm, Guillaume Loader <picpi...@hotmail.com> wrote:
> Posted viahttp://www.ruby-forum.com/.
try String#pluralize from the active_support gem
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-02-18 21:01
(Received via mailing list)
On Feb 18, 2009, at 2:39 PM, snex wrote:

>> --
>> Posted viahttp://www.ruby-forum.com/.
>
> 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
Rob@AgileConsultingLLC.com
007bac1f27905cd4d5d88315e9b0093a?d=identicon&s=25 Guillaume Loader (loader)
on 2009-02-18 22:42
I used your code Rob and it works fine! Thanks :)
This topic is locked and can not be replied to.