Question about I/O and pipes

There is a (very) little programm (count.rb) which count lines:

cnt = 0
$stdin.each_line {|line| cnt = cnt + 1 }
print cnt,"\n"

Now I want to use it whith pipes like this:
dir | count
but i got this error
C:\ruby>dir | count
C:/ruby/count.rb:3:in `each_line’: Bad file descriptor (Errno::EBADF)
from Z:/ruby/count.rb:3

What is the problem?
(NB: I an working on Windows2000.)

Thanks

“Jean-Baptiste” temuphaey0@jetable.net schrieb im Newsbeitrag
news:3fe187d2$0$7163$626a54ce@news.free.fr

There is a (very) little programm (count.rb) which count lines:

cnt = 0
$stdin.each_line {|line| cnt = cnt + 1 }
print cnt,“\n”

Now I want to use it whith pipes like this:
dir | count
but i got this error
C:\ruby>dir | count
C:/ruby/count.rb:3:in `each_line’: Bad file descriptor (Errno::EBADF)
from Z:/ruby/count.rb:3

What is the problem?
(NB: I an working on Windows2000.)

I guess that’s the problem. AFAIK there are some troubles with Windows
pipes…

robert

Hi,

···

At Thu, 18 Dec 2003 19:57:08 +0900, Jean-Baptiste wrote:

Now I want to use it whith pipes like this:
dir | count
but i got this error
C:\ruby>dir | count
C:/ruby/count.rb:3:in `each_line’: Bad file descriptor (Errno::EBADF)
from Z:/ruby/count.rb:3

What is the problem?
(NB: I an working on Windows2000.)

See [ruby-talk:84112], http://ruby-talk.com/84112


Nobu Nakada

Perl distributes a little script called pl2bat which appends a batch
file header to the script so that it can be run as a batch file, which
fixes this problem. The same works for ruby; below is your script with a
batch header. name the file with a .bat extension and it should work fine.

-----><8-----
@rem = '–-Ruby-
@echo off
if “%OS%” == “Windows_NT” goto WinNT
ruby -x -S “%0” %1 %2 %3 %4 %5 %6 %7 %8 %9
goto endofperl
:WinNT
ruby -x -S %0 %*
if NOT “%COMSPEC%” == “%SystemRoot%\system32\cmd.exe” goto endofperl
if %errorlevel% == 9009 echo You do not have Perl in your PATH.
if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
goto endofruby
@rem ';
#!ruby
#line 15

cnt = 0
$stdin.each_line {|line| cnt = cnt + 1 }
print cnt,“\n”

END
:endofruby
-----><8-----

···

On 12/18/2003 5:57 AM, Jean-Baptiste wrote:

There is a (very) little programm (count.rb) which count lines:

cnt = 0
$stdin.each_line {|line| cnt = cnt + 1 }
print cnt,“\n”

Now I want to use it whith pipes like this:
dir | count
but i got this error
C:\ruby>dir | count
C:/ruby/count.rb:3:in `each_line’: Bad file descriptor (Errno::EBADF)
from Z:/ruby/count.rb:3

What is the problem?
(NB: I an working on Windows2000.)

Thanks

Robert Klemme wrote:

“Jean-Baptiste” temuphaey0@jetable.net schrieb im Newsbeitrag
news:3fe187d2$0$7163$626a54ce@news.free.fr

There is a (very) little programm (count.rb) which count lines:

cnt = 0
$stdin.each_line {|line| cnt = cnt + 1 }
print cnt,“\n”

Now I want to use it whith pipes like this:
dir | count
but i got this error
C:\ruby>dir | count
C:/ruby/count.rb:3:in `each_line’: Bad file descriptor (Errno::EBADF)
from Z:/ruby/count.rb:3

What is the problem?
(NB: I an working on Windows2000.)

I guess that’s the problem. AFAIK there are some troubles with Windows
pipes…

robert

I get the same error for:
c:> dir | count
but it appears to work as you’d expect for:
c:> dir | ruby count.rb

Does that help?

Trey

One more time with a little less perl.

-----><8-----
@rem = '–-Ruby-
@echo off
if “%OS%” == “Windows_NT” goto WinNT
ruby -x -S “%0” %1 %2 %3 %4 %5 %6 %7 %8 %9
goto endofruby
:WinNT
ruby -x -S %0 %*
if NOT “%COMSPEC%” == “%SystemRoot%\system32\cmd.exe” goto endofruby
if %errorlevel% == 9009 echo You do not have Ruby in your PATH.
if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
goto endofruby
@rem ';
#!ruby
#line 15

cnt = 0
$stdin.each_line {|line| cnt = cnt + 1 }
print cnt,"\n"

END
:endofruby
-----><8-----

“Randy W. Sims” RandyS@ThePierianSpring.org wrote in message news:3FE28D7C.2070505@ThePierianSpring.org

One more time with a little less perl.

-----><8-----
@rem = '–-Ruby-
@echo off
if “%OS%” == “Windows_NT” goto WinNT
ruby -x -S “%0” %1 %2 %3 %4 %5 %6 %7 %8 %9
goto endofruby
:WinNT
ruby -x -S %0 %*
if NOT “%COMSPEC%” == “%SystemRoot%\system32\cmd.exe” goto endofruby
if %errorlevel% == 9009 echo You do not have Ruby in your PATH.
if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
goto endofruby
@rem ';
#!ruby
#line 15

cnt = 0
$stdin.each_line {|line| cnt = cnt + 1 }
print cnt,“\n”

END
:endofruby
-----><8-----

How is supposed to work?

With ruby 1.8.0 (2003-08-04) [i386-mswin32] I got:

F:\ruby>count < count.bat
ruby: No such file or directory – count (LoadError)

I get the same if I just type count (+ENTER) while double-clicking reads STDIN …

Confused
AA

Hmm, that’s strange. if run as ‘count.bat <somefile’ or ‘cat
somefile>count.bat’ it works, but I get a similar error ‘ruby: No Ruby
script found in input (LoadError)’ when run as you did. I haven’t seen
this behavior before, and don’t know a way around it except to use the
forms I showed that do work. :frowning:

Randy.

···

On 12/23/2003 10:06 AM, Alfio Astanti wrote:

“Randy W. Sims” RandyS@ThePierianSpring.org wrote in message news:3FE28D7C.2070505@ThePierianSpring.org

One more time with a little less perl.

-----><8-----
@rem = '–-Ruby-
@echo off
if “%OS%” == “Windows_NT” goto WinNT
ruby -x -S “%0” %1 %2 %3 %4 %5 %6 %7 %8 %9
goto endofruby
:WinNT
ruby -x -S %0 %*
if NOT “%COMSPEC%” == “%SystemRoot%\system32\cmd.exe” goto endofruby
if %errorlevel% == 9009 echo You do not have Ruby in your PATH.
if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
goto endofruby
@rem ';
#!ruby
#line 15

cnt = 0
$stdin.each_line {|line| cnt = cnt + 1 }
print cnt,“\n”

END
:endofruby
-----><8-----

How is supposed to work?

With ruby 1.8.0 (2003-08-04) [i386-mswin32] I got:

F:\ruby>count < count.bat
ruby: No such file or directory – count (LoadError)

I get the same if I just type count (+ENTER) while double-clicking reads STDIN …

Confused
AA