Take the 2-minute tour ×

So I'm trying to DRY up a Rake task which runs a script that takes user input, and I've run into the same problem as this poster - by default, just calling gets assumes that the rake argument (in this case, db:seed) is a file from which it should read, which of course doesn't exist. I got around this by just calling STDIN.gets, which works fine, but I'd love to be able to just use gets the way I can use puts (Rake seems to have no issue with STDOUT by default) - as a static method.

Is there any way to force Kernel#gets to read from STDIN within Rake? (Or more generally, is there any way to force Kernel#gets to read from STDIN when it is ostensibly passed a command line argument?) Or would that be a bad practice?

share|improve this question
This is actually general Ruby behavior, not specific to rake; i.e. any script with bare gets will try to read from a file named by a command-line option. –  echristopherson Jun 18 '13 at 21:38
There are ways, but why? STDIN.gets is a perfectly elegant solution and how it should be done. The other ways involve code adjusting $stdin which might break something else immediately, or paint you into a corner. –  the Tin Man Jun 18 '13 at 21:44
add comment

Your Answer



Browse other questions tagged or ask your own question.