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.

2 comments:

Swati Talim said...

Thanks for your compliments. Like you said, if you invest some of your time learning Ruby at RubyLearning, you will get a lot in return. All the best.

budu said...

Those god damned commas!!!