DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Fancy Little Ruby Input Prompter

  • submit to reddit
for i in 1..100
  print "Now at #{i}. Restart? "
  retry if gets =~ /^y/i


Snippets Manager replied on Tue, 2007/04/10 - 11:52pm

Might have been more Rubyish if it had been: 1.upto(100) do |num| print("\nNow at #{num}. Restart? ") retry if gets =~ /^y/ end The 'i' and for/loop were unnecessary if you use blocks. :-) Might be more optimizations, but that one sort of leaped out at me.