-
Ruby on Rails
- allyn
- June 17th, 2008
- 10:56:31 AM
We’ve recently received a number of questions about Ruby on Rails. In this post, I’m going to attempt to explain why I like Ruby and Rails. In addition to that, there’s a bonus surprise at the end!
While Ruby and Rails are often discussed as if they are one entity, they are in fact two parts: Ruby (language) and Rails (framework written in Ruby). I’ll cover each part separately.
Ruby is a fairly young language that is often described as being ‘extra-high level’, with a syntax most similar to that of Perl or Python.
The first language I learned as a programmer was Java. In learning Java, I developed a strong interest in learning other programming languages as well. I love learning how the different pieces of a language work together, and how different languages attempt to provide solutions to similar problems.
Overall, the solutions Ruby provides are very pragmatic. Some things in Java are done the way they are simply because it was the most convenient approach. Ruby offers a purpose and reason for everything. As a result, the language just makes sense.
For example, when I started learning Ruby I discovered that one could return multiple values from methods. As a Java programmer, this was a real breakthrough. I found the inability of Java to handle returning multiple values really limiting, and was really excited to take this feature for a test drive. There was one problem: I couldn’t figure out how to do it. I spent a long time (probably 10 whole minutes!) scouring the web for the answer. Eventually the right google search turned up the answer. I was expecting a code snippet thrown into some tutorial, but what I got was anything but: “Just do it”. “Just do it?” I asked. “What the hell? I want examples!” Defeated, I popped back into TextMate and tried what I figured would be the least surprising way to return multiple variables from a Ruby method. It worked!
Ruby follows the principle of least surprise. This is the biggest thing for me. I got (and am still) tired of having to guess or look up what a php method returns. I got tired of having to guess or look up the format for the name of a Java method. Ruby acknowledges this frustration and solves it by doing everything the same way every time. Think about that for a second - isn’t that what makes the most sense in programming? Why should one method be getIndex() and another be find_id()? It shouldn’t!
Ruby also solves old problems in somewhat novel ways. For example, observe the iteration of a loop in Java and Ruby:
Java: int[] list = {5, 10, 100, 6, 7}; for(int x : list){ System.out.println(x); } Ruby: list = [5, 10, 100, 6, 7] list.each do |x| puts x endThese two code blocks do exactly the same thing. Which one looks cleaner to you? Hint: the answer is Ruby.
Here’s another example that decides if any of the numbers in the list are greater then 50 and prints a boolean:Java: int[] list = {5, 10, 100, 6, 7}; boolean big_number = false; for(int x : list){ if (x > 50){ big_number = true; } } System.out.println(big_number); Ruby: list = [5, 10, 100, 6, 7] puts list.any? {|x| x > 50 }Ruby wins.
Rails is a ruby framework that was written by David Hansson.
Side note: Why the hell does this guy always include his middle name? Someone should tell that guy that no one cares that his middle name is Heinemeier. I grow weary of having to read an extra word just because some guy named David managed to convince the world to include his middle name everytime it’s written. It’s annoying!Anyway, Rails is a MVC framework. I had never really worked with MVC before, so it took me a while to get up to speed with it. Now, I can say it’s awesome. It makes a lot of sense for web development - so much so, that if I have to go back to the ‘old’ way of doing things it will be a struggle. Rails does a fantastic job of hiding the shenanigans such as maintaining database connections or writing queries from the programmer and just lets them code.
Is there a performance hit while it does all this stuff? Sure. Does it matter? Sometimes. For example, our current application has a page that executes several hundred queries when it loads. Obviously this is absolutely horrible and will have to be fixed before it’s released. However, there are ways to optimize all of Rails’ shortcomings so that the performance hit is minimal.
Sometimes, Rails pisses me off. It’s usually very easy to do easy things, but doing something new can be tricky. Robert and I have been able to solve every problem we’ve come across, and I suspect many of these little quirks will go away as the platform matures.
So that’s my analysis of Ruby and Rails. It took two hours! For those of you that have made it this far, I’ve got a little surprise for you:
SCREENSHOTS OF THE FEISTY PIRANHA PROJECT!
Click to View.Comments
Allyn and Robert
Poop?
BrightMix sees Dark Knight @ IMAX
Robert's victory pose
Hey Allyn,
Nice write-up… can’t say that I’m convinced that RoR is as awesome as everyone says it is, but I’ve certainly got a better understanding of the language than I ever have. I suppose I’ll have to get my hands dirty with it one of these days and see for myself.
Anyway, a quick comment about your 100s of queries issue. If you can’t optimize your queries and all that good stuff, I would suggest that you take a gander at memcached. Found an article on the RoR wiki that discusses it for ya:
http://wiki.rubyonrails.org/rails/pages/MemCached
And, while this is a PHP solution, it should give you some good insight into the goodness of caching:
http://www.rooftopsolutions.nl/article/107
Al, this was a great (and comical) overview of Ruby/Ruby on Rails. Some time ago, I wrote a similar (less comical) comparison of Ruby vs ASP.NET.
Also, I really like the screenshot. Reminds me of back when the app was just one singular, scribbly box instead of five. It’s amazing what you can accomplish over the course of a month with 3 full-time interns!
Ruby is just as old as Java. It appears to be younger because it was only in Japan for the majority of it’s start and in the past 4-5 years has had more popularity in the US.
http://en.wikipedia.org/wiki/Ruby_programming_language
http://en.wikipedia.org/wiki/Java_(programming_language)
Eric,
Thanks for the info and links!
Dusty
[...] to the elegance and ease of use of Ruby and the Rails framework. As Al pointed out, often times, the language just makes a lot of sense; it behaves as one would intuitively [...]
[...] you have read Allyn’s post from a week ago, you would have notice him saying: For example, our current application has a page [...]
Hey Allyn,
Thanks for the write-up. Hopefully you’ll keep us informed of the additional difficulties you run into and how you addressed them. Those things get far less air play than the “Oh, look how few lines I had to write for this”.
I do take issue with the comment about the quirks going away as the platform matures. As someone already mentioned, Ruby is quite mature, but Rails itself has also been around for almost 4 years now. That seems like plenty of time to mature, but maybe I’m being naive.
http://en.wikipedia.org/wiki/Ruby_on_Rails
Thanks again for the post,
-Derek
Derek,
Thanks for the comment! I’ll be sure to note any issues I have and see if I can compile a blog post on “stuff rails sucks at” or some such thing.
While I agree that 4 years is enough time that it should be pretty good, the fact is that it’s a complicated framework. Another thing to point out is that my issues with it are all fairly minor (so far), and it *is* a mature system already - but there’s always room for improvement. When you consider that languages such as PHP, Java and even Ruby have been around for more then a decade, 4 years suddenly doesn’t seem quite as long.
All in all, even with the minor issues that occasionally arise, it’s still awesome.
-Allyn