I’m trying to write a simple program that reads text from standard input
and writes text to standard output. The source code is as follows:
#!/usr/bin/env ruby
text = “”
while (line = gets)
text+=line
end
print text.reverse
Now I want to execute it on the Windows XP box with a pipe to redirect
results so I know that:
reverse.rb sample.txt | reverse.rb
will show me (on the screen be default) the contents of sample.txt file
in non-reversed way.
I’m executing above command line and getting this:
reverse.rb:9:in ‘gets’: Bad file descriptor (Errno:EBADF)
Do you have any ideas why it doesn’t work?
Thanks in advance for any help!
When I get that error, it usually means that I am trying to read(e.g.
gets) from something that is opened for writing only. I’m not sure how
knowing that is useful in this case.
Now I want to execute it on the Windows XP box with a pipe to redirect
results so I know that:
reverse.rb sample.txt | reverse.rb
Well, you’ve defined reverse.rb to read from stdin, but you’re passing
it a file with no stdin. I think in XP there is a “type” command that
does what “cat” does on posix systems (outputs contents of file to
stdout), so I think you want this:
type sample.txt | reverse.rb | reverse.rb
I don’t have an XP box to test on, but that works with “cat” on linux.
(Ps. Does XP’s cmd.exe support pipes “|”? Or are you using cygwin or
something similar? (If so, you may have a version of “cat”
available.))
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.