Scott Hanselman writes good stuff on programming but
his article today on what's so great about Ruby is good reading for people like me looking for an excuse to give Ruby a pass. I consider these examples to be shiboleths and i find myself squarly in the camp of those who find the "Ruby Way" to be the wrong way.
A user named yesthatmcgurk left a comment on DotNetKicks where he/she said:
I must be a complete loser, because I can't see where Ruby is such hot shit. I'd love to read a story, "What you're not getting about Ruby and why its the tits."
Such a great comment that I had to get involved. One of the other commenters pointed to a post over on "Softies on Rails" that's really worth reading.
Note: Forgive the use of "the tits" in this context. "Slang Definition: A description of something you show great liking to, or greatly appreciate..." Usually not a work-friendly phrase, but perhaps pub-appropriate.
There's a simple snippet of Ruby code:
def shutter_clicked
if @camera.off? || @camera.memory_card_full?
return
end
capture_image
end
Ruby folks have their own aesthetic and sense of beauty. They would say that the Programmer's Intent is better expressed like this:
def shutter_clicked
capture_image if @camera.on? && @camera.memory_available?
end
These two functions identically express the Programmer's Intent and the second one expresses it better, many believe.
Guess what, i don't think the second one is better, to me it's just one more example of the perl approach that the shorter the code the better the code. I don't believe that. I believe clarity is often best served by writing longer blocks (and good comments). Whenever someone shows me these kinds of statements i always show them how easy it is to properly comment/debug the longer example and not the short one.
e.g.
def shutter_clicked
if @camera.off? || @camera.memory_card_full?
// the camera either got turned off by mistake or is full
// TODO: write error handling and detect which problem
// for now let's just return without an error.
return
end
// ok, camera is ready so let's capture the image
// TODO: make sure to reset camera state later
capture_image
end
Here's another:
There are some fun one-liner comparisons though and some folks think that paying a:
Java: new Date(new Date().
getTime() - 20 * 60 * 1000)
Ruby:
In this example, the elegance is a combination of how Ruby works, and a Rails library called ActiveSupport that is a Domain Specific Language that extends Ruby. There's a special satisfaction when you read a well-written novel and you go over a turn of phrase and think, "wow, what a great way to express that. That was a perfect way to describe ____," and there's no ambiguity.
This reminds me of everything that's wrong with (the often exceptional) boost libraries for C++, a love of abusing syntax and using "domain-specific" additions to the language. Again the attraction seems to be to poetic tiny little segments of code. If you are trying to write a 5-line haiku in code, fine. But my experience is that when you start working on larger projects, you're better of knowing when you are calling a function or accessing a variable, and not introducing operator overloading and other fancy domain language extensions, etc.
Now of course this is just a minor part of Ruby, there are other things I don't like, and other things I do like, but Ruby, like most languages, seems to have developed a strong culture of style behind it that i don't fit into, so i'll keep looking.. Just my 2 cents.