Beginners Question

J

joe quimby

Hello Folks,

I am trying to learn ruby, so i wrote some simple scripts.
But the following script does not work as expected:

#!/usr/bin/ruby

while gets != nil
print $_
end

if i call 'echo.rb' (without any parameters) from my cygwin shell, the
scripts does what i expect it to do. It reads and returns the input.
But if i call it with any parameters, like

echo.rb anything

i get a error message saying : '... `gets': No such file or directory -
12 (Errno::ENOENT)...'

Does anybody understand this ?

Thanx
Joe
 
J

James Edward Gray II

Hello Folks,

I am trying to learn ruby, so i wrote some simple scripts.
But the following script does not work as expected:

#!/usr/bin/ruby

while gets != nil
print $_
end

if i call 'echo.rb' (without any parameters) from my cygwin shell, the
scripts does what i expect it to do. It reads and returns the input.
But if i call it with any parameters, like

echo.rb anything

i get a error message saying : '... `gets': No such file or
directory -
12 (Errno::ENOENT)...'

Does anybody understand this ?

gets() reads from the files given as command-line arguments, or
STDIN, if none were given. So in your example above, "anything" is
expected to be the path to a file that will be read.

Just FYI, your example is also a little Perlish. Us Ruby guys
generally write that as:

ARGF.each_line do |line|
puts line
end

Hope that helps.

James Edward Gray II
 
D

Daniel Schierbeck

joe said:
Hello Folks,

I am trying to learn ruby, so i wrote some simple scripts.
But the following script does not work as expected:

#!/usr/bin/ruby

while gets != nil
print $_
end

if i call 'echo.rb' (without any parameters) from my cygwin shell, the
scripts does what i expect it to do. It reads and returns the input.
But if i call it with any parameters, like

echo.rb anything

i get a error message saying : '... `gets': No such file or directory -
12 (Errno::ENOENT)...'

Does anybody understand this ?

Thanx
Joe

Try this instead:

while input = $stdin.gets
puts input
end


Cheers,
Daniel
 
W

Wybo Dekker

joe quimby wrote:
Hello Folks,

I am trying to learn ruby, so i wrote some simple scripts.
But the following script does not work as expected:

#!/usr/bin/ruby

while gets != nil
print $_
end

you could also have said:
#!/usr/bin/ruby
print while gets
if i call 'echo.rb' (without any parameters) from my cygwin shell, the
scripts does what i expect it to do. It reads and returns the input.
But if i call it with any parameters, like

echo.rb anything

i get a error message saying : '... `gets': No such file or directory -
12 (Errno::ENOENT)...'

Does anybody understand this ?

apparently you typed:

echo.rb 12

If there are arguments, ruby interprets those as the names of files to
be used (after concatenation) as standard input. But there was no file '12'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top