So, I’ve been doing a lot of experimentation with Ruby and Ruby on Rails recently. And I have to say, I’m really liking it.

First off, the learning curve for getting a basic app using Rails up and running is very low. Since it’s shot to the mainstream, there are many great tutorials available, as well as several great books out now and coming soon.

The main reason I like Ruby is because of it’s syntax. It’s so natural and free-flowing, and it makes perfect sense. As David Heinemeier Hansson says, it’s beautiful code. And Rails takes full advantage of that. Ruby is also completely object-based — everything’s an object, even strings and numbers. This makes it much more natural to, say convert back and forth, or run alteration methods on them:

length_in_seconds = mins.to_i*60 #convert minutes to integer and multiply by 60
person = "dave scott"
person.capitalize
puts person
# prints: Dave Scott

Even testing variables for empty values is much simpler and more natural:

if (var.empty?)
...
end

Ruby syntax includes the ? and ! operators, which give humans visual clues as to what a method does. The ? on the empty?() method tells us that this is a test — it’s asking the variable a question: Are you empty? Similarly, the ! operator, through common convention, indicates that a function does something destructive (whether that’s deleting something or changing a value permanently, or something else entirely).

And Rails makes it so easy to get everything up and running. Generators are the single greatest thing I’ve seen in web development recently. Say we wanted to create a recipe application that stored our recipes in a cookbook:

# rails cookbook
# rails script/generate scaffold Recipe

Those two simple commands will generate all the application files we need, and even generate a working CRUD interface. Of course, it doesn’t look pretty, but that’s not the point. The point is that this gives us the foundation to build our app on. We don’t have to worry about recreating database connection functions, writing and hooking up CRUD functions, or writing the code to display the interfaces for these — the generator does it for us. Now we can extend this app, while still retaining the full functionality of a CRUD interface, replacing a piece at a time.

I can’t tell you how much faster and easier the development process is using these tools, but I can try. ;) For example, I recently finished writing a web app for a local church. This web app enabled members of a program that the church sponsors to interact with each other and plan gatherings. The app is written in PHP and all told contains over 30,000 lines of code, most of which is your traditional “templatey” mess of spaghetti — PHP interspersed with HTML. It works, and it works well, but with my recent endeavors in Ruby and Rails, I decided to recreate at least a part of it in Rails to see what we can do.

So, I picked the largest portion of the site — the “call list” (this is a list of all the past attendees of the gatherings that this group holds, which is sortable, paged, and has several functions associated with each member) — to recreate in Rails. Each member can be toggled between an OK to Call state and a Do Not Call state. Each member also has comments associated with them, which are editable. There are other functions, but they are secondary. These features are the thrust of the call list, and what I was shooting to replicate.

In PHP, these functions represent roughly 900 lines of code. In Ruby/Rails, I can accomplish the same tasks in less than 100 lines of code, including display, and an AJAX request to toggle the call state. The page is identical from a user standpoint — I can toggle call state, sort, page, and view, add, and edit comments. As a bonus, I’ve also got code that allows me to add, edit, and delete attendees built-in to the app due to the Rails generator, which, in my PHP app, adds another 300 lines of code, but is less than 20 in the Ruby/Rails version.

Now, I won’t say that I’ll stop PHP development, because after all, it’s what I started in, and I still have fondness for it, but I’m beginning to come around to Mr. Hansson’s way of thinking: If I’m going to be programming day in and day out, I should be able to really enjoy it. And working in Ruby and with Rails makes it much more enjoyable. Looking at beautiful, compact, readable code that just makes sense is what it’s all about.

That all being said, once Snaps! 2.0 is out the door, look for a Snaps! on Rails to follow… ;)