1. Home
  2. Computing & Technology
  3. Ruby

Using the Command Line

By , About.com Guide

7 of 10

Use Pipe Characters to Redirect Input

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.txtto run them. Open your text editor and save the following program as test.rb.

[blockquote shade=yes] #!/usr/bin/env ruby

print "What is your name? "
name = gets.chomp
puts "Hello #{name}!"
Explore Ruby
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Ruby
  4. Tutorials
  5. Using the Command Line

©2009 About.com, a part of The New York Times Company.

All rights reserved.