This is a problem that was pointed out by Esteban Velazquez.
% cat gets2.rb
#!/usr/bin/ruby
print "getting "; puts gets
Sending the app arguments returns an error:
% ruby gets2.rb fred
gets2.rb:3:in `gets': No such file or directory - fred (Errno::ENOENT)
from gets2.rb:3
But, with no arguments, it runs ok.
% ruby gets2.rb
getting foo
foo
The problem is eluding me now. Any help would be appreciated.
Jim Freeze
It works like perl in this regard, expecting the arguments on the command
line to be files the lines of which you want read.
% echo -e "foo\nbar" > some_file
% echo "while l = gets; p l end" > gets.rb
% ruby gets.rb some_file
"foo\n"
"bar\n"
marcel
···
On Mon, Jun 12, 2006 at 03:21:18AM +0900, Jim Freeze wrote:
This is a problem that was pointed out by Esteban Velazquez.
% cat gets2.rb
#!/usr/bin/ruby
print "getting "; puts gets
Sending the app arguments returns an error:
% ruby gets2.rb fred
gets2.rb:3:in `gets': No such file or directory - fred
(Errno::ENOENT)
from gets2.rb:3
But, with no arguments, it runs ok.
% ruby gets2.rb
getting foo
foo
The problem is eluding me now. Any help would be appreciated.
--
Marcel Molina Jr. <marcel@vernix.org>
Check the documentation for the method:
http://ruby-doc.org/core/classes/Kernel.html#M002976
"Returns (and assigns to $_) the next line from the list of files in ARGV (or $*), or from standard input if no files are present on the command line."
You can explicitly use $stdin.gets or STDIN.gets to get the behaviour you want.
matthew smillie.
···
On Jun 11, 2006, at 19:21, Jim Freeze wrote:
This is a problem that was pointed out by Esteban Velazquez.
% cat gets2.rb
#!/usr/bin/ruby
print "getting "; puts gets
Sending the app arguments returns an error:
% ruby gets2.rb fred
gets2.rb:3:in `gets': No such file or directory - fred (Errno::ENOENT)
from gets2.rb:3
But, with no arguments, it runs ok.
So, I suppose that one needs to empty out ARGV before using gets?
Wow. Really?!
Jim Freeze
···
On Jun 11, 2006, at 1:36 PM, Marcel Molina Jr. wrote:
It works like perl in this regard, expecting the arguments on the command
line to be files the lines of which you want read.
% echo -e "foo\nbar" > some_file
% echo "while l = gets; p l end" > gets.rb
% ruby gets.rb some_file
"foo\n"
"bar\n"
You want IO#gets not Kernel#gets.
STDIN.gets
marcel
···
On Mon, Jun 12, 2006 at 03:44:53AM +0900, Jim Freeze wrote:
On Jun 11, 2006, at 1:36 PM, Marcel Molina Jr. wrote:
>It works like perl in this regard, expecting the arguments on the
>command
>line to be files the lines of which you want read.
>
>% echo -e "foo\nbar" > some_file
>% echo "while l = gets; p l end" > gets.rb
>% ruby gets.rb some_file
>"foo\n"
>"bar\n"
So, I suppose that one needs to empty out ARGV before using gets?
Wow. Really?!
--
Marcel Molina Jr. <marcel@vernix.org>