15 Volts
15 Volts

Reputation: 2077

How do I redirect an STDOUT to a Ruby program using pipes?

I just made a program that looks like cowsay. https://www.opendesktop.org/p/1271477/

This takes arguments, but I can't redirect stdouts to this program. What I want to do is like:

cat a_file.txt | cowspeak

Or

echo "Hello" | cowspeak

I have seen "lolcat" supports this (using trollop, I believe). I want to write it from scratch. How do I do that just in a single file?

Upvotes: 1

Views: 173

Answers (1)

Raj
Raj

Reputation: 22926

Use gets to receive the input from STDOUT via pipes.

Create a file cowspeak.rb:

puts "Cow speaks: " + gets

Demo:

❯ echo "hey there" | ruby cowspeak.rb
Cow speaks: hey there

Upvotes: 2

Related Questions