O'Reilly logo

Text Processing with Ruby by Rob Miller

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Redirecting Input from Other Processes

Standard input can be used to read text from the keyboard. You might have used it that way when learning Ruby, prompting users for input and storing the text they typed:

 
print ​"What's your name? "
 
name = $stdin.gets.chomp
 
puts ​"Hi, ​​#{​name​}​​!"

Here we ask standard input—$stdin—for a line of input using the gets method, using chomp to remove the trailing newline. This gives us a string, which we store in name.

This simplistic use of standard input isn’t particularly useful, let’s face it. But it’s actually only half of the story. Standard input isn’t just used to read from the keyboard interactively; it can also read from input that’s been redirected—or piped—to your script from another process. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required