Why I Ruby, Maybe Why You Should Too
ShareThis
April 14th 2010
Its been a little longer than I had hoped since my last post. Since I have been on vacation, working little with even less to write about, I thought I would do a little ditty on why Ruby has become my primary programming/scripting language and why I like it so much.
The Simple Answer: Its Concise Syntax
Before programming in Ruby I programmed or had programmed in C, PHP, ASP(.NET), Javascript, Perl, Java and Scheme. Each of them has their strengths and their quirks. I could never put my finger on what bothered me about each of ‘em. I still work in several of them when appropriate but when I first started toying with Ruby that feeling of 'something is missing,’ never came.
Ruby’s syntax is as close to reading plain English as it gets in my experience. Concise, easy to read code is self-documenting and requires less comments. It, also, makes code written by others (even without comments) easier to pickup. A developers style, especially one very different from yours, can make it difficult to read their code. In my opinion this is much less the case with Ruby. There is something so straight-forward about Ruby that when you pick up a foreign codebase for some light reading (‘cause we all have that extra time) it does not take much effort to figure out what is where and how it is all put together. At the same time there are many, many ways to skin your cat (mine’s polka-dotted).
When I was first picking up Ruby I came across a script that counted character frequencies in a file, a good example. Since I was new to the language I first rewrote the script in C. I, then, following the book containing the example, copied the script in Ruby. I now keep both those scripts printed out side-by-side above my desk as an example of how beautiful Ruby can be and how far programming has become.
Great Community, Great Documentation, Great Reading
Learning a new programming language is not always easy. Properietary languages like those developed by Microsoft do not always have the most accessible documentation. For old, time tested languages like C & PHP there is great documentation out there but there is even more awful or outdated content. Corporations seeking to churn out reading for developers quickly put together books without the best practices in mind, dirtying the pile. All this together makes finding examples of common solutions all the more difficult to follow and learn from. I don’t mean to say that there does not exist great reading on other languages, there does. However, just like finding a great app on the AppStore is getting more and more difficult so does finding good content the older a programming language gets.
With Ruby this is not the case and because of the community I believe it will not occur, at least, not for a while. Rubyists seem to put much more work into documentation or at least writing clear, concise examples. When googling for an answer you will typically find an RDoc right away or a well-written article. Searching for answers to a question about PHP was never as easy for me. Documentation of Ruby libraries is usually long-winded, so much so that at first glance it may seem like overkill. Then you need an answer to your ‘obscure case’ and instead of digging for hours and hours to find a credible answer you will only need seconds to find it right in the documentation or a few results down in a great article.
Some, including me, originally get thrown off by the Rails instilled motto of ‘convention over configuration’ that has spread to most of the Ruby ecosystem. In fact, its spread is one of the reasons Ruby is so great. When getting down and dirty with a new library or framework, conventions have made finding similar answers possible in similar ways. Yesterday, I needed to quickly view some metadata from my F-Spot database via HTTP. I take any opportunity I can to write a little Ruby, improve my chops, but I didn’t feel like setting up a Rails app for the few times I will ever need this functionality. I had never used Sinatra or DataMapper before but had read up on both. I figured this would be a good time to do some self-education on some great Ruby tools. So I set out to write my little application. With two quick searches for ‘datamapper sqlite3’ and ‘sinatra ruby’ I was ready to go. Within two hours I had learned two great libraries, new to me, and solved my problem. The similarties between DataMapper & ActiveRecord and Sinatra & Rack(/Rails) made the process all the easier. The great documentation filled in any blanks I needed along the way.
If your a book reader like me there are many great Ruby books and some of them are even free, online. If your already into Ruby you can download Ruby Best Practices for free. Other great starters include The Ruby Way and the ever popular Agile Web Development with Rails (4th edition now in beta). Good Ruby books are much more common than bad ones, which is normally not the case in my experience. Simply put, Ruby is one of the easiest programming languages to learn.
Have it Your Way
When I was a student in high school I had a chemistry intstructor who would start class by telling us wether or not it was going to be a Burger King Day: a day where we could work on a project of our choosing or, ‘Have it Your Way.’ We called the not fun days McDonalds days. Ironically, I prefer the golden arches when poisoning my body but I digress.
Rubyists have adopted the have it your way model and even more so recently. The redesign of Rails 3 makes the already great framework 100% ORM, test, client-side, and templating framework agnostic. ActiveRecord can be used apart from Rails, say in your Sinatra application or, like me, you can use DataMapper instead. I could have easily used another ORM as well. Nothing sums up this idea better than Ryan Tomayko’s “No require ‘rubygems’” doctrine.
Not a Language of the Web, but Perfect Enough
Unlike PHP, Ruby is not a programming language primarily interacted with via a web server. Sure, PHP CLI exists and is probably used, but most PHP code is fired via Apache, IIS, Nginx, etc. Ruby is a scripting language whose first uses were not for web development. Ruby does include WEBrick as part of its Standard Library and several other servers are distributed as gems. Its because of Rails and other Ruby web frameworks that take advantage of this that Ruby has also become a language of the web.
When developing multiple PHP applications on the same workstation each new application sandbox requires cumbersome editing of server configuration files. With Rack, Rails or any other Ruby web framework simply calling a command-line script in an extra terminal window is all that is needed to get going. To run multiple applications at the same time just change the port number when running the server script. The barrier to the web is much thinner with Ruby. Setting up a new machine to continue developing all your existing PHP applications is much more time consuming and painful than it is for Ruby code and applications. Tools like Ruby Gems and Bundler make this even easier.
In gaining popularity, Ruby has been used in applications like MacRuby, JRuby and IronRuby to facilitate the writing of Mac, Java and .NET applications respectively. Such endorsement is a huge nod to Ruby’s strengths. It shows how versatile a programming language Ruby really is. Ruby has uses for many types of problems from those solved by Apache and PHP to ones solved by a simple bash script.
Ruby changed the way I write code. Its community and tools like RSpec and Cucumber have really instilled the idea of Behavior-Driven Development in me along with many other good programming practices. The language made me a better programmer no matter which one I write in. Alan Skorkin wrote a great article about the differences between the Computer Scientist, the Programmer and the Developer. Ruby really is the language of the Programmer.