Online Book Reader

Home Category

Beyond Java - Bruce Tate [63]

By Root 674 0
:credit_card

def capture

PaymentGateway.capture(amount, credit_card)

end

end

Now in a language like Java, the direct use of PaymentGateway would be a rather nasty dependency on a concrete class that would make it hard to mock and hence test. Not so in Ruby. And especially not in Rails, since it has specific support for mocks of this type. We can stub out exactly the methods that need to be stubbed out, but nothing else. So, for the payment gateway, we'd just do this:

require "original/payment_gateway"

class PaymentGateway

cattr_accessor :desired_result

def self.capture(amount, credit_card)

Response.new(desired_result)

end

end

We're now able to specify PaymentGateway.desired_result = :success and the partially mocked out PaymentGateway will comply, using all the rest of the real infrastructure; just not calling the actual remote system over the wire.

Breaking It Down


That's a 30-minute tour through Ruby. I'm not saying that Ruby is the next great language, but rather, that Ruby makes some of the hard things in Java easy. More and more of the top independent consultants are looking for ways to make more money working in Ruby, or other languages that are more dynamic. The Java community is spending an amazing amount of money and brainpower on making Java more dynamic. Dependency injection and aspect-oriented programming are groundbreaking ideas for Java, and they are only now getting serious commercial traction. For Java developers, these ideas represent better transparency and simpler application development.

Collapsing Under the Weight of Abstraction?


My playtime in Ruby makes another, more powerful idea, clearer. As we stretch Java in increasingly unnatural directions, there's a cost. AOP and dependency injection are near-trivial exercises in Ruby, but they force Java developers to learn new programming models, deal with XML, and introduce increasingly complex syntax. With each new metaprogramming concept that we bake into Java, it's looking more and more like all of that complexity is trying to drive us somewhere. The net effect is to push Java further and further into the enterprise niche, and make it less and less accessible to the average Joe. Contrast that situation with Ruby, where dependency injection and AOP don't consume your focus; you're free to apply those ideas in spots right where you need them.

I do think that Ruby, with Rails, is a near-ideal solution for that sweet spot that we've pushed: a web-based frontend for a relational database. I've already said that I'm using Ruby in a commercial application. My customer demanded productivity and a price point that I couldn't achieve in any other way. I also still recommend Java to many of my clients. They need complex frameworks that Ruby does not yet support, or they depend on a core set of developers that have already been trained, or they have so much legacy code in Java that change would be impractical.

In the next chapter, I'll make these arguments real. I'll show you how to build a web-based application, from scratch, to access a relational database with a web application. Then, I'll show you what another killer app might be, for another language.

Chapter 7. Ruby on Rails


As I screamed uphill toward the 3-foot ledge, the voice inside my head said "Don't fight it. Go for it." Knowledgeable mountain bikers called the move the lunge, but I had neither named nor internalized it yet. My brain rebelled against the completely unintuitive idea that a moving biker could thrust his bike forward near the top of such a ledge and accomplish anything other than a spectacular crash, but I'd seen it work. I hit the ledge with speed and thrust the bike forward by simply pushing on the handlebars, and the bike was over the ledge. On some level, I didn't understand that success was a possibility. Though I was safely on top, I stepped off my pedals anyway—I'd been sure that I would fail. The idea seemed too much like flying by pulling hard enough on your shoestrings. Learning this mysterious lunge would take a while.

Like the lunge,

Return Main Page Previous Page Next Page

®Online Book Reader