Ruby on Rails for the Rest of Us
Got something to say?
Share your comments on this topic with other web professionals
In: Articles
Published on December 4, 2006
What Is Ruby On Rails?
You’ve probably heard of Ruby on Rails, which has been generating noise in the web development field over the past year. While buzz and hype are nothing new to our industry, it’s always beneficial to cut through that hype and the jargon to understand what a new technology really is about.
Ruby on Rails is an open-source web development framework that allows you to rapidly develop data-driven applications using the Ruby programming language. By applications I don’t mean software applications such as Photoshop that run on your desktop. Instead, I am referring to web applications such as Basecamp and Flickr. Like a desktop app, a web application solves a problem. For example, Basecamp lets you manage your client projects easily via the web, and Flickr lets you share photos with friends and family effortlessly.
Rails has three major goals:
- Simplicity: Developing applications should be easier, since many data-driven sites share a common set of parameters. For example, instead of focusing on writing code that will connect your application to a database, Rails handles all of that for you, thus allowing you to spend that time working with your actual application logic.
- Logical: Rails follows the Model-View-Controller (MVC) architecture for application development, which allows for a logical separation of your application logic, business rules, and user views. By keeping these things segregated, it not only makes initial development easy, it also makes updates and maintenance to your system quicker.
- Happiness: Since developing using Ruby on Rails strives to be simple and logical, it will in turn lead to a happy (and more productive) developer.
Before we investigate the Ruby on Rails framework, we should first gain an understanding of its core: Ruby. Ruby is an object-oriented, interpreted programming language. Interpreted programming languages are read line by line instead of by compiling the code into an executable that is unreadable to a human being (but is much quicker to process by a computer). Other interpreted languages include JavaScript and PHP. On the other hand, C++ and Java are compiled languages. For example, you can simply view JavaScript by viewing a web page’s source in your browser. But if you try to view the Calculator application that comes with Mac OS X, a compiled application, your text editor will only show garbage characters:
Ruby was developed in 1993 by Yukihiro Matsumoto, and first released to the public in 1995. Matsumoto designed Ruby primarily to reduce the workload of developers by following the principle of least surprise, meaning that the language typically behaves as the programmer expects: Methods are named using common English terms that appropriately define the action being performed. For example, Ruby has actions called strip, split, delete, and upcase to perform actions on strings of text. Each of those names intuitively explains the action they perform.
Ruby had a small, loyal following (small compared to the number of people interested in Ruby today) when, in late 2003, David Heinemeier Hansson and 37Signals began working on a web-based project management solution for small teams.
Initially, Hansson looked to create the application using PHP, but became frustrated with some of the shortcomings of the language. Many PHP programmers find themselves in the same shoes, repeating the same code in multiple places while building a system, for example. This process can be monotonous, redundant, and time-consuming.
Instead of succumbing to the same development process again by using PHP, he looked for a better solution, and found Ruby. Using it, he developed Basecamp in two man-months. In the process, Hansson realized that a lot of the code he was writing could be extracted into a framework that could be used as part of other future applications. In July 2004, he did just that, and released his framework, Rails, to the public.
How is it different from PHP and other languages?
The first thing you realize is that PHP is a programming language and Rails is a framework. A language is used to control the behavior of your computer using its own syntax and semantics. A framework, such as Rails, is a set of common methods, libraries, and support applications that is used as the foundation for another application. By building on top of a framework, you are given a lot of functionality for free. In other words, anything that is provided to you by the framework is fair game for use in your application without you having to implement the functionality yourself. For example, part of any web programing project usually involved writing a connector to connect your application to a database such as MySQL. With Ruby on Rails, the connector to the database is already provided, so you don’t have to spend time writing that code again. The only work you need to do is set a few options for your database login credentials.
In the past few years, development via frameworks has exploded on both the web and desktop platforms. Microsoft has been touting its .Net development framework as the new way of developing native applications for Windows XP and Vista. .Net isn’t a programming language—in fact, you can build your application with .Net using C# or Visual Basic (among other languages).
Apple also has touted framework development—their Cocoa framework hit the mainstream with the release of Mac OS X in 2001. The most popular programming language for building Cocoa applications is Objective-C. By using Cocoa, Mac developers are given a lot of functionality for free.
How difficult is Ruby on Rails to learn and work with?
The most challenging part of learning Ruby on Rails is getting used to the Ruby syntax. Let’s compare a basic “if” statement in both PHP and Ruby.
<? if ($age > 16) { echo "You are $age and capable of driving."; } else { echo "You are $age and can't drive yet."; } ?>
if age > 16 puts "You are #{age} and capable of driving" else puts "You are #{age} and can't drive yet." end
There are a few things you probably notice right off the bat:
- Ruby doesn’t wrap its decision structures in curly braces nor does it wrap the comparison in parentheses.
- Semicolons are a thing of the past.
- The Ruby code is more readable to the human eye.
There are some similarities between PHP and Rails, in that you can embed code in an HTML-based template.
<div id="greeting"> <p>Hello, you are <? echo $age ?> years old.</strong> </div>
<div id="greeting"> <p>Hello, you are <%= age %> years old.</strong> </div>
This is a very bare bones example, but it does show a few of the differences between PHP and Rails. To execute PHP in a template, you use <? ?>
whereas with Rails you have an option of using <%= %>
or <% %>
(notice the equals sign). If you attach the equals sign, the code will be output on the user’s browser. This is great when you want to show the result of an action you perform in your application: getting a list of user accounts, for example. The <% %>
escape characters are for logic code embedded in your template that doesn’t need to be /files/includes/print.cssed on the screen, such as the example below.
<% @students.each do |s| %> <p><%= s.name %></p> <% end %>
<? foreach ($students as $s) { echo "<p>$s</p>"; } ?>
In the first example, we are using Ruby’s <% %>
character sequence to tell our application to iterate through an array called @students
(an array is just a collection of objects). It then /files/includes/print.csss the name of each student on the user’s screen. PHP can do the same thing (with a bit of a different syntax), but notice that you don’t have a different character sequence to differentiate between code that is just executed on the server and what is actually shown to the user in their browser window.
If you have any experience working with a programming language (PHP or not), you shouldn’t have any trouble learning the basics of Ruby and using that knowledge to build an application using Ruby on Rails. Even if you don’t have any programming experience, getting started with Ruby on Rails is still an easy task, but you may need to learn some basic programming principles before getting started with Rails. A great introduction to programming tutorials is JoAnne Allen’s Introduction to Programming. It will teach you the basics of variables, conditionals, and methods.
What can I do with Rails?
There are several different types of applications that are being built with Rails, and they are being developed by a variety of sources. As the main creators of Ruby on Rails, it’s obvious that 37Signals is using the framework to power their suite of applications. Their main product, Basecamp, has thousands of users, and is widely seen as one of the most successful Rails applications on the market today.
Besides B2B services such as Basecamp, there are several blogging platforms that have been built on Rails. The latest is a powerful engine called Mephisto. Like WordPress, Mephisto is an open source project with developers from around the world who have helped build it into a preferred blogging solution for Rails fans.
One of the most interesting uses of Ruby on Rails is Twitter. Built by the team behind the Odeo podcasting service, Obvious Corp., Twitter allows you to keep track of your friends via SMS text messaging, IM, or the web by posting status updates. After setting up your cell phone to work with Twitter, you can then send a text message to Twitter’s SMS shortcode with a status update, and it will automatically be posted to the Twitter site. These updates can then be spread to your contact’s cell phones, so they can see what you are up to anytime during the day.
Ruby on Rails is also able to handle e-commerce. Firewheel Design’s IconBuffet allows customers to purchase icon sets using their credit cards.
Ruby on Rails is powerful enough to handle almost any type of data-driven application you throw at it.
When to use Ruby on Rails
With any new technology, people think they need to adopt it for any project they take on. Look at the number of sites that jumped on the AJAX bandwagon in the past year.
The most notable reason to build a Rails application is that you are wanting to build a data driven application easily. As I mentioned earlier, Rails has the code already baked into the framework to connect your application to a database, so you don’t have to invest any time writing common code that tends to be found in any web application. What’s more, Rails also includes a technology called scaffolding that will create a skeleton application. The scaffolding contains the models, views, and controllers. Models are the objects you are working with: a user, a real estate listing or a city. Controllers contain all the actions that your application perform. A scaffolded controller creates the basic actions to add, remove, edit, update, and show whatever type of model you are working with. Views are the actual pages that are shown to your users. They are a mix of HTML and Ruby code. It’s similar to mixing HTML with PHP includes. In fact, if you’re a designer who has built web sites using PHP includes, the process should be familiar when developing Rails templates: You are still doing the same sort of work, but with a little difference of syntax.
With scaffolding, all you need to do is define data columns and Rails can handle the rest. Scaffolding is a great way to hash out a data model quickly.
Another reason to build on top of the Rails framework, rather than starting from scratch with a language like PHP, is because time is of the essence. Since you are given so much for free with Ruby on Rails, you can rapidly develop your application in less time than with non-framework based development. Clients will appreciate the rapid development because they always need their projects finished yesterday.
Since Ruby on Rails is a new technology, if you wish to use it for client development, be prepared to learn on the job. While it’s been my goal in this article to try and ease your mind about the ease of developing with Ruby on Rails, I would be lying if I didn’t say that it can sometimes be confusing when you are first getting started, especially if you are coming from a non-framework background such as PHP. There are several outlets available to assist you with your initial Ruby on Rails development venture.
Online resources such as the Ruby on Rails Wiki and mailing list are great places to search for existing answers to common problems. There is also the #rubyonrails IRC channel on the Freenode network if you are hoping to get a quicker answer. Finally, there are several great books on Ruby on Rails available.
- Agile Web Development With Ruby on Rails – Dave Thomas and David Heinemeier Hansson
- Rails Solutions: Ruby on Rails Made Easy – Justin Williams (to be published January, 2007)
- Rails Recipes – Chad Fowler
Finally, you should only consider Rails if a solution you need doesn’t already exist. I am a proponent of not reinventing the wheel. If a solution for a problem you are trying to solve already exists, be it in Ruby on Rails or another language, investigate it before you go about trying to implement your own with Rails. For example, just because there isn’t an eBay-clone written in Ruby on Rails doesn’t mean you should write one if there is a perfectly good solution already available in PHP or another language. While it’s nice to have your own stamp in an entire code base, sometimes it’s not economical or logical to ignore a preexisting solution if it will accomplish exactly what you want.
If you find that those solutions don’t match what you need and it’s worth the time investment, I can wholeheartedly recommend you build your application using Rails. Not only is it quicker to develop via a framework, but Rails also is elegant and easy to use.
Some developers are also building static web sites as Rails applications rather than mixing static HTML and PHP includes. Personally I prefer to stick with PHP and HTML, since it’s easier to publish on a web host. Unless you need some sort of database access in your web site, there’s really no need to use Rails in this instance: Use the solution that fits the project best.
Hosting Rails
Once you develop a Ruby on Rails application, you need somewhere to host it. Not every web host supports Ruby on Rails like they do PHP. More and more hosting providers are starting to adopt Rails as their customers ask for it, but you should check with your current host to see if they support Rails before trying to deploy.
If you find your host isn’t capable of supporting Rails, you can find a host ready and willing to server your Rails application on the Ruby on Rails wiki’s web host list.
Summary
I hope this article has offered you a basic introduction to the Ruby on Rails framework. We’ve defined what Rails is and how it differs from other languages such as PHP. We’ve looked at several examples of Rails applications that are already up and running successfully, and we’ve discussed when it’s best to use the framework for your development practices.
If you think Rails is something you want to investigate more, you can get started with the framework in a no-hassle way on both Windows and Mac OS X. Windows users should look to the InstantRails project for an all-in-one solution for developing with Rails. Mac OS X users should check out Locomotive.
The goal of this article wasn’t to get you ready to start developing with Ruby on Rails, but to instead introduce you to some of the concepts and thoughts behind the framework. I’ve listed several resources that should help you with learning the framework. Good luck!
References
Ruby on Rails Homepage
Ruby on Rails Weblog
API Reference
Wiki
Mailing List
Using Rails on Mac OS X
Rolling With Ruby on Rails Tutorial
The Rails Way
Related Topics: Basics, Programming, Web Maintenance, PHP
Justin Williams, author of Rails Solutions: Ruby on Rails Made Easy, is the owner and lead developer of Second Gear, a Web and software development shop that specializes in Ruby on Rails and Mac OS X development, and founder of MacZealots.com. When not blogging or coding, you can find him watching copious amounts of TV or playing video games.