--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?
while some_action
...
end
where some_action is something that could possibly return true
forever. What I've done is something similar to:
loop_count = 0
while some_action
if loop_count > 1000
raise(RuntimeError, "Too many iterations of main loop")
else
loop_count += 1
end
...
end
Granted that works, it doesn't strike me as very "Rubyish" for
two reasons:
1) I'd think there would be a magic variable that is set already thats
an instance of the Integer class that keeps track of how many times a
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 += 1
loop_count = loop_count.next
loop_count.next! would be cool to see, same with loop_count.++
Thoughts? -sc
--
Sean Chittenden
--RvrrZ8vH9xW05bsQ
Content-Type: application/pgp-signature
Content-Disposition: inline
-----BEGIN PGP SIGNATURE-----
Comment: Sean Chittenden <sean / chittenden.org>
iEYEARECAAYFAjtV3P4ACgkQn09c7x7d+q2hUwCg1il8yiH8N0LXzg4+oJDcBgXQ
UB4AoJUbd4ynRnU0SsDCEM+FdGiZWKpL
18
-----END PGP SIGNATURE-----
--RvrrZ8vH9xW05bsQ--