Online Book Reader

Home Category

Beyond Java - Bruce Tate [57]

By Root 674 0
inside of a method. Iterating through a JDBC loop, processing a file, and iterating through a collection are only a few examples of this strategy. Some Java developers call this strategy inversion of control .

Ruby lets you program with both styles, as shown in Figure 6-1. Code written with that strategy is a joy to maintain, and it hides repetition from you. To be fair, some Java frameworks, like Spring, do some of this for you as well, but it's not as easy in Java, and this style of programming is not nearly as common, since you have to use the heavyweight anonymous inner class to do so. In dynamic languages like Ruby and Smalltalk, this programming strategy gives you tremendous intellectual freedom, both in the frameworks that you use and in the frameworks that you build.

* * *

[*] Actually, strongly typed is an oversimplification. Since you can change Ruby types indiscriminately, some might consider Ruby to have weaker typing. I'll stick with the oversimplified definition for this chapter.

Applying Some Structure


Both Ruby and Java are object-oriented languages. Both support object models with single inheritance. Still, you're going to see some differences between Ruby and Java:

Figure 6-1. Java programmers refactor the inside of a loop; code blocks let Ruby developers refactor the outside of a loop, too

In Java, the smallest application is a class. In Ruby, everything is an object, so you can evaluate primitives, expressions, code blocks, and scripts. They all are objects, and all are valid Ruby.

In Java, class definitions are static. In Ruby, you can modify your classes on the fly. When you see a class definition, if the class already exists, the new definition will modify the class that's already there.

Ruby supports mixins and Java does not. Think of a mixin as an interface, plus an implementation, that you can attach to a class.

In Ruby, everything returns some value, and that value is typed dynamically, so you won't see a return in the method definition.

In Ruby, method parameters and instance variables are not typed; but the instances themselves are typed.

For the most part, you can still use your OO design skills in Ruby as you did in Java. You'll also see some common design patterns, like model-view-controller.

David Heinemeier Hansson: Ruby

Creator of Ruby on Rails

David Heinemeier Hansson is the programmer of Basecamp, Backpack, and Ta-da List under the commercial banner of 37signals, but he's also an avid open source contributor through the Rails web development framework and Instiki—one of the most popular Ruby applications. He's intensely focused on doing something about the sorry state of programmer productivity, be it through software, like Rails, or through practices, like Less Software.

Why is Rails so much more productive than similar Java stacks?

DHH: Ruby allows Rails to implement convention over configuration at runtime, which not only removes needless repetition but also relieves the programming cycle from being bogged down by compilation, code generation, and deployment. It brings the immediacy of change-and-reload from languages like PHP together with modern software techniques like domain-driven, test-driven development, and patterns. It's quick without being dirty; it's scalable without being heavy.

What are the three most important features in Ruby that you use in Rails?

DHH: First, metaprogramming. You can manipulate a class while it's being defined. You can create domain-specific languages, because you've got hooks everywhere into the life cycle of classes and objects. It's a framework builder's dream.

Second, open classes. Active Record consists of around 10 layers that are all applied to the base class. It keeps the API simple. You don't use 10 different classes, and Rails still satisfies the requirement of a maintainable code base. It's also been helpful to be able to extend the base classes and fix bugs in the standard library between releases.

Third, everything is an object, with exceptions. You can work procedurally

Return Main Page Previous Page Next Page

®Online Book Reader