Forum: Ruby Beginners Question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
37fdd6ee8f6f631168894cc57bdbcdba?d=identicon&s=25 Joe Quimby (jughead)
on 2005-12-29 16:18
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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-29 16:24
(Received via mailing list)
On Dec 29, 2005, at 9:18 AM, joe quimby wrote:

>
> 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
37fdd6ee8f6f631168894cc57bdbcdba?d=identicon&s=25 Joe Quimby (jughead)
on 2005-12-29 16:28
Wow that was fast. Thanx a lot
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2005-12-29 16:33
(Received via mailing list)
Hi --

On Fri, 30 Dec 2005, joe quimby wrote:

> Wow that was fast. Thanx a lot

A quick followup:  You can do:

   $stdin.gets

which will force (in the case of your example) keyboard input.


David

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2005-12-31 12:06
(Received via mailing list)
joe quimby wrote:
>
>
> Thanx
> Joe
>

Try this instead:

   while input = $stdin.gets
     puts input
   end


Cheers,
Daniel
B7f73e9f9d35be6a44b15f603f2910d4?d=identicon&s=25 Wybo Dekker (Guest)
on 2005-12-31 13:18
(Received via mailing list)
On Sat, 31 Dec 2005, Daniel Schierbeck wrote:

> 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'
This topic is locked and can not be replied to.