[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

gets bug?

Shea Martin

1/4/2006 3:15:00 PM


<snip>
if ARGV.size > 0
puts "first arg is #{ARGV[0]}"
end
puts "enter text"
ans = gets
puts "you said #{ans}"
</snip>

Run this code, as ./test.rb. Then run it with an command line arg, like
"./test.rb -x". And you will get an error like this:

"test.rb:3:in `gets': No such file or directory - -x (Errno::ENOENT)"

The fix I have found, is to change gets to $stdin.gets. I saw some
traffic on the mailing list archives regarding this, but no resolution.

Is this expected behaviour?
ruby -v is: ruby 1.8.3 (2005-09-21) [sparc-solaris2.10]

Thanks,

~S
3 Answers

James Gray

1/4/2006 3:27:00 PM

0

On Jan 4, 2006, at 9:17 AM, Shea Martin wrote:

>
> <snip>
> if ARGV.size > 0
> puts "first arg is #{ARGV[0]}"
> end
> puts "enter text"
> ans = gets
> puts "you said #{ans}"
> </snip>
>
> Run this code, as ./test.rb. Then run it with an command line arg,
> like "./test.rb -x". And you will get an error like this:
>
> "test.rb:3:in `gets': No such file or directory - -x
> (Errno::ENOENT)"
>
> The fix I have found, is to change gets to $stdin.gets. I saw some
> traffic on the mailing list archives regarding this, but no
> resolution.
>
> Is this expected behaviour?

Yes it is. Kernel.gets() reads from files specified as command-line
options or STDIN, if none were given. This makes it easy to write
Unix-style filters.

You can always shift the option out of ARGV before calling gets()
with something like:

if ARGV.first == "-x"
ARGV.shift
# ...
end

Hope that helps.

James Edward Gray II



Gary Watson

1/4/2006 3:36:00 PM

0

Shea Martin wrote:
>
> <snip>
> if ARGV.size > 0
> puts "first arg is #{ARGV[0]}"
> end
> puts "enter text"
> ans = gets
> puts "you said #{ans}"
> </snip>
>
> Run this code, as ./test.rb. Then run it with an command line arg, like
> "./test.rb -x". And you will get an error like this:
>
> "test.rb:3:in `gets': No such file or directory - -x (Errno::ENOENT)"
>
> The fix I have found, is to change gets to $stdin.gets. I saw some
> traffic on the mailing list archives regarding this, but no resolution.
>
> Is this expected behaviour?
> ruby -v is: ruby 1.8.3 (2005-09-21) [sparc-solaris2.10]
>
> Thanks,
>
> ~S

I've run into this problem before as well, somebody pointed me to the ri
documentation on Kernel#gets, it says the following.

Returns the next line from the list of files in +ARGV+, or from standard
input if no files are present on the command line. Returns +nil+ at end
of file.

Hope this helps.

Robert Klemme

1/4/2006 5:10:00 PM

0

James Edward Gray II wrote:
> On Jan 4, 2006, at 9:17 AM, Shea Martin wrote:
>
>>
>> <snip>
>> if ARGV.size > 0
>> puts "first arg is #{ARGV[0]}"
>> end
>> puts "enter text"
>> ans = gets
>> puts "you said #{ans}"
>> </snip>
>>
>> Run this code, as ./test.rb. Then run it with an command line arg,
>> like "./test.rb -x". And you will get an error like this:
>>
>> "test.rb:3:in `gets': No such file or directory - -x
>> (Errno::ENOENT)"
>>
>> The fix I have found, is to change gets to $stdin.gets. I saw some
>> traffic on the mailing list archives regarding this, but no
>> resolution.
>>
>> Is this expected behaviour?
>
> Yes it is. Kernel.gets() reads from files specified as command-line
> options or STDIN, if none were given. This makes it easy to write
> Unix-style filters.
>
> You can always shift the option out of ARGV before calling gets()
> with something like:
>
> if ARGV.first == "-x"
> ARGV.shift
> # ...
> end

I'd prefer to use OptionParser.parse! for this - but otherwise totally
agree.

robert