October 2007 Archives

Fluent Programming in Ruby

| 3 Comments

Matz has just checked a change into the Ruby 1.9 tree to allow programming with fluent interfaces. The change is marked "experimental", but if it stays in the tree, we'll be able to write Ruby code like this:

puts "hello"
        .upcase
        .reverse
        .slice(0)    # => Prints "O"

(This is a silly example; Martin Fowler's page linked above has more compelling examples of this style of method chaining.)

We've always been able to do method chaining in Ruby, of course. What Matz's patch does is allow newlines in between the methods. This is tricky in languages like Ruby that don't require semicolons because newlines usually act as the statement terminators. So with this new modification a newline does not terminate a statement if the first non-space character on the following line is a period.

P.S. In case it hasn't been obvious from my recent postings on Ruby, I'm writing a book about Ruby. And I'm nearly done!

Last to Die

Bruce Springsteen, on Iraq, 2007:

Who?ll be the last to die for a mistake
The last to die for a mistake
Whose blood will spill, whose heart will break
Who?ll be the last to die for a mistake

John Kerry, on Vietnam, 1971:

Each day to facilitate the process by which the United States washes her hands of Vietnam someone has to give up his life so that the United States doesn't have to admit something that the entire world already knows, so that we can't say that we have made a mistake. Someone has to dies so that President Nixon won't be, and these are his words, "the first President to lose a war."

We are asking Americans to think about that because how do you ask a man to be the last man to dies in Vietnam? How do ask a man to be the last man to die for a mistake?

Books

Comprehensive coverage of Ruby 1.8 and 1.9

"The New Most Important Ruby Book"
Peter Cooper,
rubyinside.com

Completely updated for Ajax and Web 2.0

"A must-have reference"
Brendan Eich,
creator of JavaScript

The classic Java quick-reference