Skip to Content
Ruby Pocket Reference
book

Ruby Pocket Reference

by Michael Fitzgerald
July 2007
Beginner to intermediate
176 pages
2h 30m
English
O'Reilly Media, Inc.
Content preview from Ruby Pocket Reference

The IO Class

The basis for all input and output in Ruby is the IO class, which represents an input/output (I/O) stream of data in the form of bytes. Standard streams include standard input stream ($stdin) or the keyboard; standard output stream ($stdout), the display or screen; and standard error output stream ($stderr), which is also the display by default. IO is closely associated with the File class, and File is the only standard subclass of IO in Ruby. I’ll show you a sampling of IO code.

To create a new I/O stream named ios, use the new method. The first argument is 1 which is the numeric file descriptor for standard input. Standard input can also be represented by the predefined Ruby variable $stdin (see Table 8). The optional second argument, w, is a mode string meaning write:

ios = IO.new( 1, "w" )

ios.puts "IO, IO, it's off to the computer lab I go."

$stdout.puts "Do you copy?"

Table 8. Standard streams

Stream description

File descriptor

Predefined Ruby variable

Ruby environment variable

Standard input stream

0

$stdin

STDIN

Standard output stream

1

$stdout

STDOUT

Standard error output stream

2

$stderr

STDERR

Other mode strings include r or read-only (the default), r+ for read-write, and w for write-only. For details on all available modes, see Table 9.

Table 9. I/O modes

Mode

Description

r

Read-only. Starts at the beginning of the file (default mode).

r+

Read-write. Starts at the beginning of the file.

w

Write-only. Truncates existing file to zero length, or creates a new file for writing.

w+

Read-write. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Ruby Pocket Reference, 2nd Edition

Ruby Pocket Reference, 2nd Edition

Michael Fitzgerald
Ruby Phrasebook

Ruby Phrasebook

Jason Clinton
Ruby by Example

Ruby by Example

Kevin C. Baird

Publisher Resources

ISBN: 9780596514815Errata Page