1. Computing

IO Quick Reference

By

IO Quick Reference

The IO class is the core class that all input and output methods, such as files or network sockets, inherit from. This article covers a few ways to create IO classes (opening files and network connections) as well as a few of the method that can be called on them.

Default IO Objects

As soon as your Ruby program starts, there are a few IO objects already open. These can be used whenever needed.

  • $stdout or STDOUT is a write-only IO object whose output goes to the terminal (AKA standard output, or if the output is piped to a file or program, to that file or program). Any calls to the puts method without any object (for example, the statement puts "Hello World") will use this object.
  • $stdin or STDIN, like the previous object, maps to standard input. Any calls to the gets method without an object specified will read from this object.
  • $stderr or STDERR is much like $stdout, but will write to the standard error stream instead. This is useful for reporting error messages, warning, or just information that's not part of the program output proper. This allows the user to pipe the program output to a file or another program while error messages will still be displayed in the console.

Opening Files and Sockets

  • Opening files is done via the File.open method. This method returns an object of type File, which is a child class of type IO. Generally, this method takes two arguments, two strings specifying the filename to open and the mode for opening the file. Generally, the arguments follow one of these two patterns. To open a file for reading, you use file = File.open("filename.txt", "r"). This will raise an exception if the file does not exist. To open a file for writing, erasing all the contents of the file and starting the cursor at the beginning of the file, use file = File.open("filename.txt", "w+"). This will also create the file, should it not yet exist.
  • Opening network sockets is rather simple in Ruby. To connect to a remote host, use the TCPSocket.new method. It takes two arguments, the hostname and the port. To open a connection to a web server, use sock = TCPSocket.new("someserver.com", 80). If you're coming from a language like C, this should seem too simple to be true, but that's all you really need.

Reading Strings

Once you have a file or socket open, it's time to read some string data. This is done with a few methods.

  • Reading lines is done via the gets method, just like from the console. If you have file = File.open('somefile.txt', 'r'), then you can read a line from this file with line = file.gets.
  • Reading all the lines from a file is done using the readlines method. If you have file = File.open('somefile.txt', 'r'), then you can read all the lines from a file as an array of strings with lines = file.readlines. There's also a shortcut for this. The method call lines = File.readlines('somefile.txt') will open the file, read all the lines into the array then close the file in a single method call. This is a common way to read the lines of a file in Ruby.
  • Reading the entire file in a single string is done with the read method. If you leave all the parameters blank, the read method will read the entire file. If you have file = File.open('somefile.txt', 'r'), then you can read the entire file into a string using entire_file = file.read. Again, as this is a common operation, there's a shortcut for it. The File.read method will open a file, read its contents, and close it. So instead of the above, you can say entire_file = File.read('somefile.txt').

Writing Strings

Writing strings is a simple, and generally only comes in two varieties: writing a string with a newline, and writing a string without a newline.

  • To write a string with a newline, use the puts method, just as you would with writing a string with a newline to the console. If you have file = File.open('somefile.txt', 'w+'), you can write a string plus a newline to it with file.puts "Some string".
  • To write a string without a newline, use the write method. If you have file = File.open('somefile.txt', 'w+'), you can write a string without a newline following it to the file with file.write "Some string".
  1. About.com
  2. Computing
  3. Ruby
  4. Beginning Ruby
  5. IO Quick Reference

©2014 About.com. All rights reserved.