There are many good books and online tutorials on Ruby so this short chapter will concentrate on just what you will need for this book. This chapter will briefly introduce you to the Ruby features like collections, strings, defining classes, and I/O. I assume that you have Ruby installed, and that you have an irb session open while working through the examples in this chapter.
Note to readers: this chapter will be expanded as I write the rest of the book. This chapter will only document the specific features of the Ruby language used in this book and should not be considered a complete introduction.
Ruby is an object oriented programming language, even primitive types like integers.
Figure 1.1 shows a UML class diagram showing commonly used Ruby data classes.
|
Given any Ruby object, it is easy to interactively discover its class:
Produces the following output:
The ri program is run interactively like irb to print documentation on methods or classes. Being able to quickly check what methods are available for a class or object, along with the ri utility makes it easy to get started using Ruby.
One powerful feature of Ruby is that all classes are ”open”, that is, you may add methods to any classes. As a contrived example, we will add a method my_double to the standard String class:
You see the use of the self keyword here: self refers to the object that the method is being called on. This new method now works for any string object::
Ruby arrays can grow dynamically as needed and array elements can be any type of Ruby object.
Produces the following output:
Ruby defines operators for arrays in a natural way. For example, you can use the - operator to ”subtract” elements from one array from another:
Produces the following output:
We have been using puts and pp to print values to standard output. These work well if you are not printing too many items; the following code would print available methods one per line:
The methods Array#join (this means method join of class Array) converts each element of an array to a string and concatenates these strings together using any ”join string” that you supply. Here, we generate a string with available method names separated by ”, ”:
This code produces the following output:
You can create string constants by either using double or single quotes. Strings created with single quotes are lighter weight and slightly more efficient but do not support embedded formatting. Here are a few examples:
This code produces the following output:
One of the most powerful features of the Ruby language is the ability to define code blocks. The following listing shows two examples of passing code blocks defined inside curley brackets to the methods each and collect:
This code produces the following output:
Blocks are in effect ”anonymous” methods that do not require a name because they are used explicitly in one place in your code.
You can also write your own methods to accept code blocks. In the following example, we check to see if a code block was provided and if one was provided, execute it:
This code produces the following output:
Ruby supports Mixins to add behavior to classes. Enumerable is used to add useful behavior to collections for testing the contents of collections, iterating through collection elements, searching for specific elements, partitioning collections into two disjoint sets, and for sorting.
We saw the use of two Ruby iterators in the last section: each and collect. In both examples, these iterators were passed a code block. The combination if iterators and blocks, which we will explore more in this section, allow us to write code that is shorter and more readable than in languages like Java that support a variety of iterators but not code blocks.
Iterators allow us to perform operations on each element of a collection without having to write code dealing with the type of collection or what types of objects are in a collection. The following long example shows off the the techniques used in the rest of this book:
This code produces the following output:
Hash tables implement associative memory: hash keys map into values associated with each key. Both hash keys and values can be arbitrary Ruby objects: numbers, strings, arrays of simple values, etc.
This code produces the following output:
Here is another example that shows how to define a default value for a hash table: requesting the value for a key that is not in the hash table produces the default value.
This code produces the following output:
Fie I/O is simple in Ruby. The following example shows how to write strings to a file, read them back, and how to delete the test file: