Infinate Loop - Please Advise

This program produces an infinate loop. I am learning from Learn to
Program and do not have a clear example how to do this particular
example. It is suppose to count down the bottles and repeat the phrase
until it reach 0 bottles of beer then end :slight_smile:

Merrie

beers = 99
beerno = beers - 1.to_i
while beerno != 1
puts '99 bottles of beer on the wall, take one down, pass it
around, there are ’ + beerno.to_s + ‘bottles of beer on the wall.’

end

You never decrement beerno.

SonOfLilit wrote:

beers = 99
beerno = beers - 1.to_i
while beerno != 1
puts '99 bottles of beer on the wall, take one down, pass it
around, there are ’ + beerno.to_s + ‘bottles of beer on the wall.’

end

You never decrement beerno.

At least not within the while loop.

A: Because it makes it hard to read replies.
Q: Why is top-posting bad?


Phillip “CynicalRyan” Gawlowski
http://cynicalryan.110mb.com/

Rule of Open-Source Programming #33:

Don’t waste time on writing test cases and test scripts - your users are
your best testers.

You have no looping structure.

block of code for loop should be contained by
{…}
for one-liners, or
do … end
for multi-line blocks.
try:

beerno =99
 while beerno > 0 do
     puts "#{beerno} bottles of beer on the wall, take one down,

pass it around,"
beerno = beerno - 1
puts “#{beerno} bottles of beer on the wall.”
end
end

of course, there are many ways to build this same looping/iterating/
enumerating structure!
some are more rubyistic than others.

here’s another example, using downto

beer = 99
beer.downto(0) do |b|
puts “#{b} bottles of beer.”
puts “take one, drink it.”
puts “#{b - 1} left.”
end

but that needs an if statement to take care of the negative beer!

beer = 99
beer.downto(0) do |b|
puts “#{b} bottles of beer.”
if b > 0 do
puts “take one, drink it.”
puts “#{b - 1} left.”
end
end

Of course it could be shorter still. but that’s for you to toy with.

John J. wrote:

but that needs an if statement to take care of the negative beer!

Of course it could be shorter still. but that’s for you to toy with.

why wouldn’t you just have beer.downto(1) and ditch the if statement?

Sorry, here’s a better working example. I wrote the others quickly in
irb. The trouble with irb is that it isn’t the most graceful
environment for writing nested conditions or loops or elements. It’s
possible, but easy to make mistakes. Anyway, the white space in the
conditionals and looping constructs is pretty important to Ruby.

beer = 99
beer.downto(1) do |cerveza|
puts “#{cerveza} bottles of beer on the wall,”
puts “#{cerveza} bottles of beer…”
puts “take one down pass it around…”
if cerveza > 1
puts “#{cerveza - 1} bottles of beer on the wall”
else
puts “no mas cerveza…”
end
end

Oh…silly me, thank you all!

Merrie
----- Original Message -----
From: “John J.” [email protected]
To: “ruby-talk ML” [email protected]
Sent: Monday, April 09, 2007 12:18 PM
Subject: Re: Infinate Loop - Please Advise

On Apr 10, 2007, at 3:03 AM, Raj S. wrote:

Of course it could be shorter still. but that’s for you to toy with.

why wouldn’t you just have beer.downto(1) and ditch the if statement?

I did, a few moments later. doing that stuff in irb isn’t always as
easy as doing it in TextMate (note to self).
I like irb because I can just ask objects questions. But irb needs
more flexibility (the ability to step into and out of writing a block
to check something else, that would be useful) Access to all the
running stuff and ability to change it while running. Oh.
self.assend_to_heaven

Yeah! A SmallTalk-like environment would be wonderful.
irb is already pretty cool.
But it always seems to be just short of what it could be.
I kinda wish TextMate had its own irb session ability, then it could
be in color and have lots of cool tricks too.
But these are dreams and wishes, because I am not about to write
anything like this myself. Not that clever.

On Apr 9, 2:17 pm, John J. [email protected]
wrote:

  • snip -
    I did, a few moments later. doing that stuff in irb isn’t always as
    easy as doing it in TextMate (note to self).
    I like irb because I can just ask objects questions. But irb needs
    more flexibility (the ability to step into and out of writing a block
    to check something else, that would be useful) Access to all the
    running stuff and ability to change it while running. Oh.
    self.assend_to_heaven

So basically, you want Smalltalk :slight_smile:

If you’re unfamiliar, Smalltalk lives in an ‘image’ which offers the
flexibility you describe, with a full-GUI IDE environment – you can
inspect and manipulate live objects and code as you develop, test and
debug.

I’ve actually considered building a similar environment for Ruby,
should I find the time and as wxRuby becomes more stable. As a matter
of fact, I believe I remember reading on here that FreeRIDE was
developed with that particular aim in mind.

On one hand it seems superfluous, since Smalltalk already has one, but
on the other it would be a great deal of fun to do so, and it seems
like a fit with the language given Ruby’s Smalltalk heritage.

On Apr 9, 2007, at 10:54 PM, John J. wrote:

I kinda wish TextMate had its own irb session ability, then it
could be in color and have lots of cool tricks too.

Two things:

  1. TextMate does ship with xmpfilter wrapped in a command, so it’s
    already possible to evaluate statements and see the results, while
    keeping all the editing advantages of TextMate. See Bundles → Ruby -

Execute and Update ‘# =>’ Markers.

  1. I wrote an article about how to build interactive environments
    inside of TextMate. The techniques used in it, could be used to make
    an “IRbMate.” It should be published next week on http://
    www.macdevcenter.com/.

James Edward G. II

On Apr 10, 2007, at 9:34 PM, James Edward G. II wrote:

Ruby → Execute and Update ‘# =>’ Markers.

  1. I wrote an article about how to build interactive environments
    inside of TextMate. The techniques used in it, could be used to
    make an “IRbMate.” It should be published next week on http://
    www.macdevcenter.com/.

James Edward G. II

look forward to seeing it.
Kinda waiting to see what happens with the next version of TextMate
too. Needs better multilingual support. This is the only thing I can
really really be disappointed about in TM. Quite ironic that the
premier Ruby and Rails editor in the west will barf on Japanese
input, and that it is such a wonderful example of a good Mac app yet
doesn’t have the language support so well provided by Cocoa…
now we’re way OT