So, I have written a little application called cowspeak
It does various jobs on a GNU/Linux system. But the problem is while accepting inputs using pipes.
I have already implemented accepting IO redirection with this code:
#!/usr/bin/ruby -w
require 'timeout'
STDIN.sync = STDOUT.sync = true
pipe = false
begin
Timeout.timeout(0.000_000_000_001) do pipe = STDIN.gets end
rescue Timeout::Error
end
if pipe
print pipe
print pipe while pipe = STDIN.gets
end
It's all fine.
So for echo -e "hello\nworld" | ruby cowspeak
, I get
hello
world
But the problem is it doesn't work with programs like irb (yes, I don't need to use IRB with this program, but I want to learn how it should work). A working example is the lolcat
gem.
So for irb | ruby cowspeak
, I get it running forever in the while
loop (implemented in loop
loop in cowspeak).
Lolcat works fine in that case as well!
Also, apps like cmatrix doesn't work with my program, but they work with lolcat.
Unix programs like cowsay (which is written in Perl) doesn't also work with pipes well - the same behaviour as cowspeak...
Anyways, I see the question is asked before here: ruby pipes, IO and stderr redirect
But it doesn't answer my question properly.
How can I implement IO redirection with Ruby and overall how the lolcat gem really work with pipes?