Using Pipe CharactersUsing the pipe characters is an important skill to master, as these characters will alter the input or output of a Ruby script. In this example, the > character is used to redirect the output of test.rb to a text file called test.txt instead of printing to the screen.
If you open new test.txt file after you run the script, you'll see the output of the test.rb Ruby script. Knowing how to save output to a .txt file can be very useful. It allows you to save program output for careful examination or to be used as input to another script at a later time.
C:\scripts>ruby example.rb >test.txt
Similarly, by using the < character instead of the > character you can redirect any input a Ruby script may read from the keyboard to read from a .txt file. It's helpful to think of these two characters as funnels; you're funneling output to files and input from files.
C:\scripts>ruby example.rb <input.txt
Then there's the pipe character, |. This character will funnel the output from one script to the input of another script. It's the equivalent of funneling the output of a script to a file, then funneling the input of a second script from that file. It just shortens the process.
The | character is useful in creating "filter" type programs, where one script generates unformatted output and another script formats the output to the desired format. Then the second script could be changed or replaced entirely without having to modify the first script at all.
C:\scripts>ruby example1.rb | ruby example2.rb
Continue to The Interactive Ruby Prompt.