[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Beginners Question

joe quimby

12/29/2005 3:18:00 PM

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

--
Posted via http://www.ruby-....


5 Answers

James Gray

12/29/2005 3:23:00 PM

0

On Dec 29, 2005, at 9:18 AM, 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
>
> 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


joe quimby

12/29/2005 3:28:00 PM

0

Wow that was fast. Thanx a lot

--
Posted via http://www.ruby-....


dblack

12/29/2005 3:32:00 PM

0

Daniel Schierbeck

12/29/2005 5:47:00 PM

0

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
>
> 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

Wybo Dekker

12/31/2005 12:18:00 PM

0