I/O streams and the IO class
An input/output stream is a sequence of data bytes that are accessed sequentially or randomly. This may seem like an abstract, high-level concept -- it is! I/O streams are used to work with almost everything about your computer that you can touch, see, or hear:
- printing text to the screen
- receiving key-press input from the keyboard
- playing sound through speakers
- sending and receiving data over a network
- reading and writing files stored on disk
All of these listed operations are considered "side-effects" in Computer Science. The touch/see/hear metric doesn't seem to work for network traffic and disk activity but side-effects are not necessarily obvious; in these two cases, something in the world has physically changed even if you can't see it.
Comparatively, "pure" code is code without side-effects: code which simply performs calculations. Of course, a "pure" program isn't very useful if it can't even print its results to the screen! This is where I/O streams come in. Ruby's IO
class allows you to initialize these streams.
fd
, the first argument to IO.new
, is a file descriptor. This is a Fixnum
value we assign to an IO
object. We're using a combination of the sysopen
method with IO.new
but we can also create IO objects using the BasicSocket
and File
classes that are subclasses of IO
. We'll learn more about File
in the next lesson.
I warned you it would feel a bit abstract! The notion of creating a "file descriptor" is inherited from UNIX, where everything is a file. Because of this, you could use the above technique to open a network socket and send a message to another computer. You wouldn't do that, of course -- you would probably use the BasicSocket
(or perhaps TCPSocket
) class we just mentioned.
Let's leave the abstract behind and find something a little more concrete. There are a bunch of I/O streams that Ruby initializes when the interpreter gets loaded. The list here may seem longer than if you run this locally. These examples are evaluated in the dense rubymonk environment consisting of Rails, Passenger, and all our other magic juice.