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!
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!
On Feb 18, 1:29 pm, Guillaume L. [email protected] wrote:
Posted viahttp://www.ruby-forum.com/.
try String#pluralize from the active_support gem
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
I used your code Rob and it works fine! Thanks
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs