Wednesday, August 25, 2010

From Clojure to Ruby

Ever since I've received Stuart Halloway's Programming Clojure, I've been reading, watching and listening about Clojure. And dabbling with it a little.

A few months ago, I started learning Ruby. My main learning experience has been The Ultimate Book to Ruby Programming by Satish Talim and his free online class Core Ruby.

I just finished the Core Ruby Class 18th batch and I must say it's excellent. The class material not only makes a good introduction, it's also my first reference for looking up core concepts. The class ran for 8 weeks. At first, I thought that was too long to cover the basics, but if you do have a day job, the class will be taking plenty of evening and weekend study time.

Each week has assigned reading materials, exercises and a quiz. To really get more from the class than you'd get from just reading a book, you have to do the exercises, post your solutions, discuss them and read solutions from others. The class have competent and friendly mentors that will really help you get from a "Classic OO language in Ruby" solution to an idiomatic Ruby solution. I've also had the chance to have good classmates.

I must stress the point that you can only get back from the class as much as you invest yourself in it. Workload would vary from week to week, but I consider 5 hours a week an absolute minimum and 10 hours is better. I've once spent 8 hours just on a bonus exercise (The Playfair Cypher, if you must know.) YMMV of course!

The bottom line is if you're serious about learning Ruby and you're ready to put in the hours and your passion, the Core Ruby Class is an excellent way to start your journey.

Going back to Clojure, I got some advantages from learning it that translate to Ruby since they have some things in common.

The first thing I noticed is both languages use keywords and with the same syntax too! Hello, I'm a :keyword. They are called symbols in Ruby.

Keywords can be seen as a way to use strings as constants. The advantage over using normal strings is two symbols with the same name are guaranteed to be the same object and thus, you can compare using reference equality instead of value equality. It's typical to use keywords for hash keys. It took me a while to wrap my head around the keyword concept in Clojure, so I was glad to be able recycle it.

Another common thing is the use of separators instead of camel cases for variable and function names: encrypt_message in Ruby or encrypt-message in Clojure. Also, putting ! or ? at the end of the function to express mutation or a predicate. Example: "Hello".empty? in Ruby or (empty? "Hello") in Clojure. Funnily enough, I'm testing these examples with Redcar, a Ruby editor running on JRuby featuring both a Ruby and a Clojure REPL.

As for conditional testing, Ruby and Clojure considers false and nil to be false, and everything else to be true.

Well, there are plenty of differences too of course and I got bitten more than once. They say when you learn your third spoken languages that you'll mix it up with your second spoken language at first. Likewise, even though one is a Lisp dialect and the other one isn't, more often than not, I'll declare a Ruby function like this:
def repeat_function some_function time_interval total_interval do...

I forget the commas between the arguments!
I'll have the same problem calling a function too.

Strange, but true.

Sunday, August 8, 2010


There's a new Sherlock Holmes series on BBC One. It's simply named Sherlock.

I'm a huge fan of the Sherlock Holmes series played by Jeremy Brett as Sherlock and Watson being played first by David Burke an then by Edward Hardwicke. All excellent actors and to me, the character as played by Jeremy Brett became THE Sherlock Holmes.

When doing a new adaptation of a classic, there's always a fine line to walk. You want to respect the original and at the same time, you want to do variations to make it fresh and interesting.

I found the 2009 Sherlock Holmes movie starring Robert Downey Jr. quite excellent, although I had accepted beforehand that it would be different.

Now I'm finally getting back to the new BBC One Sherlock. The story takes place in present-day Lodon, which is really interesting. Sherlock and Watson are wonderfully played by Benedict Cumberbatch and Martin Freeman. This character version of Sherlock Holmes has definitively some variations from the character played by Jeremy. Without giving spoilers, Sherlock performs a cruel action in the final confrontation that would not be compatible with Jeremy Brett character version. On the other hand, he seems less like a robot. He makes jokes!

The episodes are movie-length: 90 minutes! I watched the first episode twice and enjoyed it a lot. I liked the second episode a bit less, although I can't say why.

All in all I think this new series is a tremendous achievement with the visuals, sounds, scenario and acting. I'm really looking forward to the third episode. I guess the only down-side is it seems we can't expect other episodes for a while after that.