noob question

R

rjtucke

I'm learning Ruby and was wondering if there's an elegant way to do the
following:
(Program is to print the 99 bottles of beer song, with correct english)
if num != 1
word = "bottle"
else
word = "bottles"
end

I tried to do this via word = "bottle" + ("s" unless num == 1),
but it complains about concatenating "bottle" with nil.

Thanks,
rjtucke
 
R

Ryan Eibling

rjtucke said:
I'm learning Ruby and was wondering if there's an elegant way to do the
following:
(Program is to print the 99 bottles of beer song, with correct english)
if num != 1
word = "bottle"
else
word = "bottles"
end

I tried to do this via word = "bottle" + ("s" unless num == 1),
but it complains about concatenating "bottle" with nil.

Thanks,
rjtucke

How about word = "bottle" + (num == 1 ? "" : "s")
There's probably something better but that takes care of the nil thing.
 
S

Sri Sankaran

Here's one way to skin this cat:

word = (num == 1) ? "bottle" : "bottles"

Sri
 
M

Martin Nemzow

Just use the built-in Ruby/Rails plural method if your bottle count > 1
Don't reinvent the wheel.
 
K

Karl von Laudermann

Martin said:
Just use the built-in Ruby/Rails plural method if your bottle count > 1
Don't reinvent the wheel.

Rails is not "built-in" to Ruby. It's a separate third party web
application framework. Downloading and installing a whole web
application framework just to pluralize a word for a "99 Bottles of
Beer" program would be beyond ludicrous.
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top