Review of Black, Ruby for Rails, Chapter 4

As others have mentioned, Chapter 4 of David A. Black’s book Ruby for
is now available for download from the Manning site. The fact
the book is being released one chapter at a time makes it easy to read
small, digestible chunks. I never intended to review every chapter
individually here, but now that I’ve done the first three, I feel like I
ought to continue.

I should mention, though, that doing this isn’t being very fair to the
author. It’s wildly unlikely that he ever anticipated someone doing
and no doubt didn’t expect the individual chapters to be evaluated
one-by-one in that manner. Hopefully the fact that these reviews bring
the book repeatedly in this forum will be worth it. Besides, so far I
like the book. :slight_smile:

Chapter 4 begins the second part of the book, which is all about the
of the Ruby programming language. As a longtime Java developer, this
chapter reminds me a lot about the spate of introductory Java books that
the market in the mid-90’s when Java was new and the idea of OO
still needed to be motivated. I wonder how true that is any more.
it’s true that there are many, many programmers on this list who are
from non-OO based languages, my experience is that most of the
development going on in industry these days is in one OO language or
another, whether it be Java, one of the .Net languages, or whatever.
There’s even an OO version of COBOL available these days, though that
doesn’t change the fact that there are roughly 4 trillion lines of COBOL
the industry that know nothing about that. Still, an introductory
on OO programming in Ruby is not a bad thing.

Since I’ve been teaching OO programming for years (and associated topics
like OOAD, UML and design patterns), I didn’t really focus on the basic
aspects of the chapter. Instead, the areas that caught my attention
the things that make Ruby different from other languages I know.

One of those differences emerges almost immediately. In his first
right off the bat, he does something you can’t do in Java, which is to
methods to an object from an existing library class. He creates an
of the Object class, naturally enough, and then proceeds to define a
method and a “c2f” method on that instance. In Java you would have to
extend Object (i.e., create any class at all) in order to do that; here
able to add methods to an object that already exists that came from a
that already exists. Coming from a Java background, I’d have to say
that is
both seriously cool and somewhat vertigo-inducing. Since the author is
trying to compare Ruby to anything and is assuming no previous OO
on the part of the reader, he doesn’t emphasize this. He just builds
object and the methods and goes on.

Of course, the other strange thing about those method definitions when
viewed by a Java programmer is that they don’t have any return types.
whole issue of dynamic typing, however, is left for a later chapter.

The author draws the common distinction between “sending a message” and
“calling a method” on an object, which I always felt was more semantics
anything else. Still, it’s certainly correct. There is also the
interesting Ruby characteristics that parentheses are optional in method
calls and that the last evaluated expression is in fact the return value
from a method.

The best feature of this of book, as I’ve mentioned before, is that it
promises to dig into Rails for its Ruby examples. That doesn’t happen
this chapter. Instead, the object example that is chosen is a “ticket”,
which is defined in terms of attributes. Then he proceeds to build a
“toString()” method in terms of string interpolation (embedding values
“#{object.attribute}”). He also shows how Ruby methods that return
often have names that end in a question mark. This leads into the fact
everything in Ruby is true except for “false” and “nil”. He even shows
a “puts” expression returns nil (using irb) and how that may be
for a developer to know.

There is also a discussion of object identity and object equality. This
leads to a discussion of some of the other methods from the Object
like “responds_to?” and “send”. I found that helpful, because I
need to learn the Ruby libraries and this is an easy introduction to

The next section talks about required, optional, and default-valued
arguments. Until version 5.0 (or 1.5, depending on your point of view),
Java had nothing like that. In this area, Ruby feels more like a
powerful OO relative to JavaScript.

The last section of the chapter introduces the idea of references to
and how assignment with references causes both references to point to
same object. This also brings up the idea of local references inside
methods and how they can screen more global references.

One other point that is mentioned more than once in the chapter is how a
single word by itself (“barewords” as referred to by the author) can be
either references or method calls, assuming they aren’t keywords. That
transparency hearkens back to Eiffel (or maybe just Bertrand Meyer’s
Object-Oriented Software Construction book, and is one of the truly
(and confusing) things about Ruby.

In conclusion, this chapter is a “back to basics” chapter, introducing
programming to a novice audience. If that describes you, it’s great.
not, there are still interesting aspects of Ruby to be gleaned from here
that will no doubt be further highlighted as the book progresses.

Ken K.

Kenneth A. Kousen, Ph.D.


Kousen IT, Inc.

mailto:[email protected] [email protected]