From: Sean Chittenden Date: 2001-07-19T04:01:27+09:00 Subject: [ruby-talk:18060] Best way to prevent infinite loops... --RvrrZ8vH9xW05bsQ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Howdy. What's the best way to prevent infinite loops in Ruby? =20 while some_action ... end where some_action is something that could possibly return true=20 forever. What I've done is something similar to: loop_count =3D 0 while some_action if loop_count > 1000 raise(RuntimeError, "Too many iterations of main loop") else loop_count +=3D 1 end ... end Granted that works, it doesn't strike me as very "Rubyish" for=20 two reasons: 1) I'd think there would be a magic variable that is set already thats=20 an instance of the Integer class that keeps track of how many times a=20 loop has been executed. Is there and I just haven't found it yet? 2) The following two work, but aren't very graceful: loop_count +=3D 1 loop_count =3D loop_count.next loop_count.next! would be cool to see, same with loop_count.++ Thoughts? -sc --=20 Sean Chittenden --RvrrZ8vH9xW05bsQ Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Comment: Sean Chittenden iEYEARECAAYFAjtV3P4ACgkQn09c7x7d+q2hUwCg1il8yiH8N0LXzg4+oJDcBgXQ UB4AoJUbd4ynRnU0SsDCEM+FdGiZWKpL =mJ18 -----END PGP SIGNATURE----- --RvrrZ8vH9xW05bsQ--