[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

noob question

rjtucke

6/7/2006 7:57:00 PM

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

6 Answers

Ryan Eibling

6/7/2006 8:08:00 PM

0

rjtucke wrote:
> 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.

Sri Sankaran

6/7/2006 8:10:00 PM

0

Here's one way to skin this cat:

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

Sri
"rjtucke" <rjtucke@gmail.com> wrote in message
news:1149710220.028556.144830@i40g2000cwc.googlegroups.com...
> 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
>


rjtucke

6/7/2006 8:16:00 PM

0

sweet. Thanks
rjtucke

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

Daniel Schierbeck

6/7/2006 9:03:00 PM

0

rjtucke wrote:
> if num != 1
> word = "bottle"
> else
> word = "bottles"
> end

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

Cheers,
Daniel

Martin Nemzow

6/8/2006 1:06:00 PM

0

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

"rjtucke" <rjtucke@gmail.com> wrote in message
news:1149710220.028556.144830@i40g2000cwc.googlegroups.com...
> 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
>


Karl von Laudermann

6/9/2006 1:39:00 PM

0

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.