James Gray
7/17/2008 1:15:00 PM
On Jul 17, 2008, at 3:15 AM, Devious Forlan wrote:
> I've started learning Ruby and I'm really, really enjoying it. I
> have no
> prior experience with any programming language, and I've been reading,
> listening and watching as much Ruby related material I can.
Great. Welcome to Ruby!
> Can someone please take a look at my code, it's s simple loop as per
> Chris Pine's Learn to Program book.
Sure, I can take a look.
> This is what I've come up with to print the 99 bottles poem
>
> -----
> 01| bottles = 99
> 02|
> 03| while bottles != 0
> 04| puts bottles.to_s + ' of beer on the wall! ' + bottles.to_s +
> ' of
> beer!'
> 05| puts 'Take one down, and pass it around, now there is..'
> 06|
> 07| bottles = bottles - 1
> 08|
> 09| if bottles == 0
> 10| puts bottles.to_s + ' of beer on the wall'
> 11| puts bottles.to_s + ' bottles of beer!'
> 12| end
> 13|
> 14| end
> -----
Looking good to me.
> Can someone tell me if I'm on the right track?
>
> For example.
>
> 09| if bottles == 0
>
> I've seen people use the following instead
>
> Ex| if bottles != 1
>
> What's the the most correct context because I'm still wrapping my head
> around this.
As you said, the two are equivalent. There's no right or wrong way to
represent it. It's probably best to use whichever one you feel makes
the code the most clear. However, I don't think there's much
difference for this particular case.
Again, nice job and welcome.
James Edward Gray II