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.
Guillaume L. (Guest)
on 2009-02-18 21: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!
snex (Guest)
on 2009-02-18 21:40
(Received via mailing list)
On Feb 18, 1:29 pm, Guillaume L. <removed_email_address@domain.invalid> wrote:
> Posted viahttp://www.ruby-forum.com/.
try String#pluralize from the active_support gem
Rob B. (Guest)
on 2009-02-18 22: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 B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
Guillaume L. (Guest)
on 2009-02-18 23:42
I used your code Rob and it works fine! Thanks :)
This topic is locked and can not be replied to.