From: Daniel Bye Date: 2012-04-04T20:35:46+09:00 Subject: Re: Newbie loops question --sdtB3X0nJg68CQEu Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Apr 04, 2012 at 08:26:23PM +0900, Bryan Bales wrote: > I am learning Ruby as my first programming language using the excellent > tutorial by Chris Pine http://pine.fm/LearnToProgram/ >=20 > Everything was going well until I tried to write a simple program to > print out the 99 bottles of beer on the wall song. Here is my code. >=20 > int =3D 99 > while int !=3D 1 > puts int.to_s + ' bottles of beer on the wall, ' + int.to_s + ' > bottles of beer' > int.to_i - 1 > puts 'Take one down, pass it around, '+int.to_s + ' bottles of beer on > the wall' > end >=20 > When I run this, I get into a loop of 99. It never subtracts one. I have > figured out all the other programs that you are to try, but for some > reason the problem in this is escaping me. Any help will be greatly > appreciated. You're not actually storing the result of subtracting 1 from int anywhere, so as far as Ruby is concerned, it never changes. You want something like=20 int =3D int - 1 instead of int.to_i - 1 Note that calling to_i on int is not really necessary here, since int already contains an integer. Dan --=20 Daniel Bye _ ASCII ribbon campaign ( ) - against HTML, vCards and X - proprietary attachments in e-mail / \ --sdtB3X0nJg68CQEu Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (FreeBSD) iEYEARECAAYFAk98MgwACgkQixf5fBYiFmpqYwCg3rkrxVsGLyw7NxxqvZSf/hiO IKIAn0EgY+NBHnHU3aMs4YBbIWGsny/6 =m3NZ -----END PGP SIGNATURE----- --sdtB3X0nJg68CQEu--