Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

noob question

0 views
Skip to first unread message

rjtucke

unread,
Jun 7, 2006, 3:57:00 PM6/7/06
to
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

Ryan Eibling

unread,
Jun 7, 2006, 4:07:43 PM6/7/06
to

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

rjtucke

unread,
Jun 7, 2006, 4:16:04 PM6/7/06
to
sweet. Thanks

Sri Sankaran

unread,
Jun 7, 2006, 4:10:24 PM6/7/06
to
Here's one way to skin this cat:

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

Sri
"rjtucke" <rjt...@gmail.com> wrote in message
news:1149710220.0...@i40g2000cwc.googlegroups.com...

Daniel Schierbeck

unread,
Jun 7, 2006, 5:02:33 PM6/7/06
to
rjtucke wrote:
> if num != 1
> word = "bottle"
> else
> word = "bottles"
> end

word = "bottle" << (num == 1 ? "" : "s")

Cheers,
Daniel

Martin Nemzow

unread,
Jun 8, 2006, 9:06:06 AM6/8/06
to
Just use the built-in Ruby/Rails plural method if your bottle count > 1
Don't reinvent the wheel.

"rjtucke" <rjt...@gmail.com> wrote in message
news:1149710220.0...@i40g2000cwc.googlegroups.com...

Karl von Laudermann

unread,
Jun 9, 2006, 9:39:15 AM6/9/06
to
Martin Nemzow wrote:
> 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.

0 new messages