Thread.new in a console

17 Mar 2016

Suppose you've got a million books and you want to transmogrify all of them, and transmogrification takes somewhere between 5ms and 1s. You might hop into a console and do:

books.each {|b| b.transmogrify! } ; nil

Only problem is that locks up the console for a long time and you have to figure out some out of band way of tracking progress. Well, here's a pretty low-tech, but nice, way around that:

done = [] ; Thread.new { books.each {|b| done << b.id ; b.transmogrify! } } ; nil

Now you can do done.size whenever you want to see how far things have progressed. Crude but effective!