RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Ruby—A Diamond of a Programming Language, Part 2 : Page 7

Get ready to dive deeper into the power and elegance of the language lauded as a possible contender to replace current programming languages.

Throughout this article and the last Ruby article, you have written Ruby code that has used a number of Ruby standard output methods. The methods of print and puts were often used, but the details were glossed over.

It turns out that these methods, and several others for dealing with input and output, are defined in the Kernel module. The Kernel module is included by the Object class. Therefore, the Kernel's methods are present in every object. On the output side, Kernel defines print, printf, putc, and class and two subclasses (File and BasicSocket) that allow reading and writing to files and sockets. BasicSocket is part of the socket library and will be discussed later. The File class, which includes the FileTest module, provides a number of methods to work with system files (as its class name implies) and directories (which the name of the class does not imply). The methods used from Kernel to write/read to the standard input/output mechanisms are reused to also write and read from instances of File. Below is a code sample to write some names in a newly created file and the corresponding code to read the names back out of the same file into an array.

customers=%w[Jim Kevin Davin Andrew]
outFile = File.new("c:\\examples\\test\\customers.txt", "w")
customers.each{|customer| outFile.puts(customer)}
inFile= File.new("c:\\examples\\customers.txt", "r")
readCustomers.each{|customer| puts customer}
Standard Libraries
Along with Ruby's rich set of built in classes and modules, Ruby also comes with a number of standard libraries. These libraries are not automatically part of the built in Ruby classes, modules, methods you can take advantage of. You will first have to use the require (or load) keyword at the top of your file to use the classes or modules in the library. I mentioned one of those libraries in the previous section—the socket library, which contains a number of Ruby classes (including BasicSocket) for accessing network services. But there are a whole host of other libraries provided with the Ruby download. Have a look in the lib directory of your Ruby download. In the lib directory, you are looking at the many Ruby libraries available to you and your Ruby applications.

The unfortunate part about the libraries is that there is not a lot of documentation on many of these classes. You will find a list of the standard libraries and their files containing classes and modules at http://www.ruby-doc.org/stdlib/. Even this documentation goes to the length of indicating that:

“What you need to know is that 
	bold libraries in the table of contents are well documented, and 
	italic libraries are not.”
Alas, this is the current state of Ruby. As you can hopefully tell, Ruby is an incredibly rich and powerful language with built in support (through libraries) for many of the functions we need our applications to do, but documentation is still somewhat limited. The good news is that there are a number of people working to improve Ruby's documentation and support. A number of Ruby forums have sprouted up and the documentation improves with each new release—a result, no doubt, of its recent rise in attention. However, it can still be one of the somewhat frustrating aspects of the language.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date