From: Mark Hubbart Date: 2005-06-07T05:44:29+09:00 Subject: Re: 'gets' has been hijacked On 6/6/05, James Edward Gray II wrote: > On Jun 6, 2005, at 2:31 PM, Pete Elmore wrote: > > > I was working with a simple script, that unexpectedly broke. The > > problem can be reduced to this: > > > > pete@robots:~$ cat t.rb > > #!/usr/bin/env ruby > > p gets > > pete@robots:~$ ./t.rb > > asdf > > "asdf\n" > > pete@robots:~$ ./t.rb nonexistent-file > > ./t.rb:2:in `gets': No such file or directory - nonexistent-file > > (Errno::ENOENT) > > from ./t.rb:2 > > > > Ah, right, I remember this from the Bad Old Days of Perl: stdin has > > been stolen. Is there any way to stop this behavior, like a global > > I could set or a flag I could pass to ruby? Couldn't find anything > > on the man page or in Pickaxe. > > gets() as you're using it calls Kernel.gets(), which basically > amounts to ARGF.gets(). That's very handy for building Unix filters. expanding on that: ARGF *is* STDIN when there are no files specified: mark@eMac% ruby -e'p ARGF.fileno == STDIN.fileno' true mark@eMac% ruby -e'p ARGF.fileno == STDIN.fileno' /etc/hosts false > If your intention is to read from the keyboard only, just make sure > call gets() on STDIN with STDIN.gets() or $stdin.gets(). > > Hope that helps. > > James Edward Gray II > >