Review of Black, Ruby for Rails, Chapter 4

As others have mentioned, Chapter 4 of David A. Black’s book Ruby for
Rails
is now available for download from the Manning site. The fact
that
the book is being released one chapter at a time makes it easy to read
in
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
this
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
up
the book repeatedly in this forum will be worth it. Besides, so far I
really
like the book. :slight_smile:

Chapter 4 begins the second part of the book, which is all about the
details
of the Ruby programming language. As a longtime Java developer, this
chapter reminds me a lot about the spate of introductory Java books that
hit
the market in the mid-90’s when Java was new and the idea of OO
programming
still needed to be motivated. I wonder how true that is any more.
While
it’s true that there are many, many programmers on this list who are
coming
from non-OO based languages, my experience is that most of the
server-side
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
in
the industry that know nothing about that. Still, an introductory
chapter
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
OO
aspects of the chapter. Instead, the areas that caught my attention
were
the things that make Ruby different from other languages I know.

One of those differences emerges almost immediately. In his first
example,
right off the bat, he does something you can’t do in Java, which is to
add
methods to an object from an existing library class. He creates an
instance
of the Object class, naturally enough, and then proceeds to define a
“talk”
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
he’s
able to add methods to an object that already exists that came from a
class
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
not
trying to compare Ruby to anything and is assuming no previous OO
background
on the part of the reader, he doesn’t emphasize this. He just builds
the
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.
The
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
than
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
in
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
in
“#{object.attribute}”). He also shows how Ruby methods that return
booleans
often have names that end in a question mark. This leads into the fact
that
everything in Ruby is true except for “false” and “nil”. He even shows
how
a “puts” expression returns nil (using irb) and how that may be
significant
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
class,
like “responds_to?” and “send”. I found that helpful, because I
obviously
need to learn the Ruby libraries and this is an easy introduction to
them.

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
souped-up,
powerful OO relative to JavaScript.

The last section of the chapter introduces the idea of references to
objects
and how assignment with references causes both references to point to
the
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
cool
(and confusing) things about Ruby.

In conclusion, this chapter is a “back to basics” chapter, introducing
OO
programming to a novice audience. If that describes you, it’s great.
If
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.

President

Kousen IT, Inc.

http://www.kousenit.com http://www.kousenit.com

mailto:[email protected] [email protected]