2005 / December 21st/ Are you a Rails developer on the inside?
Rails has been a large influence on my life in the past year. I know it sounds strange to say such a thing, but I’m serious. Rails has taught me that programming doesn’t have to be hard. There can be a kind of beauty in code–a poetry in the syntax. It was not the amazing productivity statistics that wooed me. It was not the proven track record of paradigm-shifting applications like Basecamp. It was the search for something better.
The PHP Ghetto
Back in the Winter of 2000 I decided to start learning ASP (not .NET mind you, we’re talking 3.0) in order to make a game with some friends. It never became a reality, but that vision of a game lead me undoubtedly to PHP, which was quickly overtaking ASP at that time. It’s so easy to fall into the PHP trap. It’s just too easy to do things. A couple of mysql_query’s and you’ve got yourself a dynamic database-driven web site.
But that doesn’t mean beauty. My code was ugly and it simply got me by. My techniques changed project to project and day to day. Even my database structures were all over the map. But I kept on going. I continued delving into PHP until I thought I knew a little bit about programming. By this time it was 2003 and I was talking with one of my friends about building something cool. I started PWM in PHP the first time around with a pretty sleek XML/XSLT powered backend. While the technologies used were impressive, the beauty was lacking. There was just too much work involved in doing the simplest of tasks. I was stuck in the PHP ghetto, too involved in the technologies to think about what I was writing.
One day I decided to finally try out Ruby on Rails. I never turned back.
Ruby: code is poetry
Ruby is a beautiful language. I don’t think any developer can deny this. My entire experience with programming until Ruby had been with languages like ASP (3.0), PHP, C, MATLAB, and JavaScript. I was used to strange braces, parenthesis and all kinds of strange characters floating about. In Ruby, a reasonable default is almost always assumed. While it’s perfectly legal to pollute Ruby code with curly braces, parenthesis and semicolons–they’re not required. If there is no question to a function’s inputs - there’s no need for parenthesis around your arguments.
The result is that your code reads like words out of a book. No longer do you feel obligated to write comments in your code when it reads like this:
class Poem < ActiveRecord::Base
belongs_to :author
has_many :comments
validates_uniqueness_of :title, :content
validates_presence_of :title, :content
end
Can you guess what the above code does? Any other person even somewhat familiar with programming languages could pick up this code and work with it. It just feels right.
The little things in life
I’ll leave you with a few, very simplistic snippets of code in Ruby. Ruby deals with Arrays and Hashes unlike any language I’ve dealt with before (granted, there are a number of languages that act similar — I’ve simply not used them). Since arrays and hashes are so common in programming, it makes sense.
# Create an array of numbers
numbers = [1, 2, 3, 4]
# The following will cycle through the number array, and print out each value, unless numbers is not defined.
numbers.each{ |number| puts number } unless numbers.nil?
# Create a new time object with the value of right now
time = Time.now
# I'd like 'MM/DD/YYYY' please
puts time.month.to_s + '/' + time.day.to_s + '/' + time.year.to_s
It’s all the little things in life that make me happy. I love being able to put things like time.day in code and get what I expect–the day of the time object. I don’t like using date() and converting all of my time objects into seconds since the unix epoch. It just doesn’t sit right with me.
I’ve only scratched the surface of Ruby, but every time I dig a little deeper I find a new respect for the language. Its code resembles that of human beings, and not that of machines. Remember, you write code for people–not machines.
Rails
Rails is, of course, what lead me to Ruby, and it’s success cannot be underestimated in bringing Ruby into a commonly used language. While Ruby has been around a very long time it’s always missed that spark, that wildfire that really gets languages into public usage. Rails was that fire, and it keeps burning brighter every day.
Rails, how shall I count the ways I love thee? I cannot express what a wonderful framework Rails is. It just takes the hard out of programming. It makes development a dream. When you spend far less time actually writing code than you do thinking about how to build your application, you know you’re sitting on a solid foundation.
I believe Rails works so well because it was architected by one person. It was not programmed by committee, its features not pre-set before inception–it was architected as the code base demanded it. Since the entire base of the framework was written by one person it has a sense of fluidity and consistency that many frameworks (and programs for that matter) lack. There is a clear vision, and clear opinions about the way software should be built.
I latched on to those opinions and haven’t been happier since. Rails doesn’t leave me time to think about where I should be storing my files, or how I should name my database tables. It sets down a paradigm lead by experience and guides my own architecture. The real value here is that your own code becomes more consistent because of it. No longer are you wondering how your file structure is laid out because it’s already there for you. No longer are you wondering how you named your database columns, because there’s templates for that. To me, this is what put me on the rails, so to speak, towards good programming practices.
I could go on about how the framework has increased my productivity, or given me a newfound respect for David. But seriously, what’s the use? The end result is that I love programming in Rails. It’s given me a passion for programming I did not think I had. It’s driven me to learn more about Ruby and allowed me to do things I never thought I could. That’s the end result for me: programming is actually fun.
In pursuit of beauty
David put together a great set of slides for a presentation titled “Pursuing beauty with Ruby on Rails.” I think this presentation is an amazing representation of what Rails is all about. The fact that nearly the entire presentation is composed of actual Rails code makes you wonder why other frameworks/languages can’t produce snippets like these. That’s what Rails is about. Pursuit of beauty.
Should you try out Rails?
If you haven’t tried out Rails, I would of course recommend it. I think anyone who programs for any decent amount of time should play with it at some point. It’s not for everyone, and it definitely is not the answer to every single project. But for most projects, and for a lot of people it’s a great fit. Even if you chose to walk away from Rails you will undoubtedly walk away a better programmer for it.
Where to get started?
There are a growing number of resources out there for Ruby on Rails newbies. The best investment I could suggest is Agile Web Development with Ruby on Rails. It was co-written by Dave Thomas and David Heinemeiser Hansson (Rails’ father) and gets you started right away building an application. If you’re short on cash or just want to play around, the wiki is an excellent place to start out as well.
Don’t forget that the Rails community is one of the nicest and brightest groups of people I’ve ever seen before. Their passion for code is amazing. If you ever get stuck, drop on by IRC on #rubyonrails and I’m sure someone would be glad to help you out.

Warpspire is the place that web professional Kyle Neath writes about the web. 



December 21st | #
I’m actively teaching myself Ruby right now. I’ve come far, and the jump fom PHP wasn’t hard at all, especially with Rails. But I still love PHP. And I cannot stress enough how unfair it is to compare PHP to Ruby on Rails. Framework to No Framework isn’t a fair comparison.
Anyways.. Rails is pretty, easy, modular, flexible, simple, and lean. I like it. I just wouldn’t say its an end all be all. Though I am sure, fairly soon, that we will see the same shift in both acceptance and use that we saw during the ASP > PHP transition.
Very nice post. It does well to both introduce and explain rails, a noble effort to say the least.
December 21st | #
> Even if you choose to walk away from Rails you will undoubtedly walk away a better programmer for it.
I feel much the same way. Good article!
December 21st | #
I’m sorry but even though this is an excellent article I fail to ’see the light’. I don’t see how Ruby code is more ‘beautiful’ than any other code. It just looks different to me that’s all. I can write butt ugly code in PHP, I can write beautiful code in PHP. It all depends on how you do your stuff.
And if you ‘walk away’ from it you MIGHT be a better programmer I think, not necessary ‘will’. This depends on the programming practices you were using before looking into Rails.
Like Koray said, the comparison PHP vs. Rails which has been made by a gazillion webloggers is totally unfair and not even a valid one. It’s the apples / pears thing. The two just can’t be compared.
Also I found Rails to be a horror to install compared to PHP and any PHP library/toolkit.
I’d like to see a shootout between Symfony or Wasp (php frameworks with the same goals as Rails) versus Rails That would actually be a fair comparison. I don’t think the difference is going to be all that significant.
Still a nice article even though I don’t really agree!
December 21st | #
It’s exciting when you find that system that allows you to be creative and to develop. I think I shared many of the same sentiments as you express here back when I started building within the confines of my own framework in PHP.
> Remember, you write code for people?not machines
Hmm.. Here’s you are talking about Ruby, which is coded for a machine, really. We build apps (interfaces?) for people :-)
December 21st | #
I feel the main problem with Ruby on Rails - and the main reason, why it still makes sense to stay with PHP - is the fact, that most co-hosting/shared server providers don’t allow for Ruby on Rails and especially the FastCGI. At least here in Germany I don’t know any provider who allows it on a reasonably priced (= student friendly) hosting plan.
December 21st | #
Sorry I posted that twice. First time commenter here and I was totally not prepared for AJAXy comments.
December 21st | #
Excellent article as always Kyle!
Some quick tips. Try this for your second example:
(1..4).each do |num|
puts num
end
And this for your third:
Time.now.strftime("%m/%d/%y")
Also, incase you were unaware you can also do things like
Time.now.yesterdayandTime.now + 2.days.Marco - The thing is that you can’t knock it until you try it. Most people fail to see the difference because they don’t take the time to try and tackle problems with it or get familiar with it.
Kyle’s article only touches the surface in regards to the beauty of Ruby, having powerful tools such as
method_missingand open Modules and Classes are some other things. Sure, showing a few lines of code from one Class leads you to believe that you can get all this done with Php (and thats probably true), but when you start getting a larger code base you can tell just how much Ruby paired with Rails makes your life easier. So I think the question isn’t that “Hey, I can do this in Php as well”, it’s how those problems are solved in Ruby compared to Php.The installation factor is probably the biggest complaint from most, because unless your extremely patient or a server admin it can be overwhelming in some cases. This really has nothing to do with Ruby or Rails itself. The reason Php is so easy to install is because most of the time it’s already there, and you don’t need Fastcgi.
Php has been in the mainstream a lot longer than Ruby, and in due time the problems with installing Ruby and friends will be greatly reduced.
Cheers
December 21st | #
Great article Kyle. I have been dipping my toes into the Ruby pool little by little over the last month or so just trying out some of the available tutorials around the web. I’ve been overly impressed with how the code is written and what can be done is such a short period of time using RoR.
It seems you’ve got a ton of passion for RoR and I hope you wouldn’t mind some IM chat on the subject in the near future if you have some time?
December 21st | #
Justin, thanks for the input. The numbers one was a bad example since I could have easily used a range, but I was trying to show a generic array :) But yes, there are so many lovely “shortcuts” in Ruby it’s hard to explain them all. Not to mention ActiveSupport, which as always impresses me to no end. Things like Time.now.end_of_month or 5.odd? is simply amazing to me. Again, the little things :)
As for installing, it all depends. A lot of it is because Ruby is new territory for people. I remember the first time I tried to install PHP locally it took me two weeks. It only took me two hours with Ruby, and now after doing it — I can set it up on anyone’s machine in under ten minutes.
December 21st | #
I think that more than the beauty of the code, what I appreciate about Rails is the inherant structure. Like you said, there are preset rules of how things should be done. It takes out the guesswork, and lets you focus on what matters. I think that this is something CSS could benefit from. CSS code is beautiful for the most part: border-top = a top border, etc. However, because it’s so flexible, people that are trying to pick it up find themselves floundering with questions like “What should I name this div?” Anyway, it’s the apples / pears thing again, but my basic point is that more programming frameworks and languages in general could benefit from more structure.
December 24th | #
Just out of curiosity, what paradigm did Basecamp shift? Might just be the proliferation of clones, but it seems like a pretty standard app to me…
Nice article.
December 24th | #
Yes, but is
validates_numericality_of :post_id
all that beautiful?
:P
December 28th | #
So the code is beautiful. Okay. But does that mean no other language can have nice looking code? I agree, PHP doesn’t have the best looking code — though I can make it myself — but it gets the job done.
Honestly, trust me, I have nothing against Ruby. I just love the languages I use currently: Python, which I think have even nicer code than Ruby and PHP which is just so damn easy to use — But even still, I take the time to write nice OOP code, commenting using PHPdoc and overall MAKING the code beautiful..
December 28th | #
uzair>>
No one can deny that 37signals have redefined what a web application is. That’s what I’m talking about.
Mark Anthony>>
Pssh. Why not debate how to pluralize octopus? ;)
Veracon>>
I completely agree beauty is up to the developer, and how he creates his code. But there is something to say about the language’s ability. I’ve not played with Python much, so I really didn’t try to compare it to Ruby. I believe many languages, similar to Ruby, are capable of creating wonderful code. However, I think there are also a lot of languages that restrict the freedoms that allow such beauty. In my experience, PHP is one of them. There is absolutely no way to create a call as elegant as this in PHP:
A lot of this can be attributed to Ruby’s lax syntax rules. A lot of it to method_missing. A lot of it to pure OO. PHP (and other languages) simply cannot compare.
December 28th | #
I am a big fan of RoR as well and am using it on a few projects of my own. I have actually rewritten most of the RoR ActiveRecord and ActionController in PHP to be able to use it on projects at my workplace (where there is no chance of moving to Ruby due to heavy oracle legacy db and stored procedures).
I tend to favor the OO in ruby as well, and that is one of the things that attracts me to the language, but honestly there is no reason you could not write very similar code in PHP5 with some use of the magic methods like __call() and __get(), and some SPL for making an object traversable with foreach:
$user = User::find_by_username_and_password('kneath', 'password');
foreach ($user->articles as $article) { print $article->title; }
Is this example really that much uglier than the ruby syntax? You have to remember that the “find_by_username_and_password” and “user.articles” are not native Ruby. These are parts of Rails/ActiveRecord, and can be easily emulated in most dynamic languages.
I think there is a lot of FUD going around about PHP from people who don’t quite understand the full capabilities. I think we’ll see some better comparisons once Zend releases their framework. I don’t believe that it will be real competition for Rails since a lot of PHP developers seem to reject the idea of the framework.
December 29th | #
Firstly, I haven’t tried Rails yet (I know a little Ruby, though), so I can’t say how or even if I would like the framework, but judging from what I’ve seen over on the main website, people’s comments, similar to this, and what I’ve read, it seems pretty neat, and I will be checking it out. It’s the simple things, however, that make me not like it. I couldn’t get it to install. Something funky was going on with gems. It’s such a barrier to productivity and new faces to the framework that it’s almost funny; and simultaneously it is something that should be so easy to fix.
I again agree that the PHP/RoR comparison is not fair; there is no way that you can compare PHP to a development framework.
Judging by what you said, I think you would actually like Python, which, I believe produces beautiful code. Not as beautiful as Ruby, which I believe is the only language that I have used apart from BASIC that truly doesn’t need commenting. In my opinion, that is what makes beautiful code. Python also has some fantastic frameworks, TurboGears and Django come to mind. I’ve tried the former, and it is absolutely brilliant.
Someone mentioned it above - I too would like to see a critical comparison between Rails and a PHP framework such as Symfony. I haven’t tried Symfony yet, and I’ve been torn between it and Rails. A link to someone who’s compared them?
December 29th | #
I certainly don’t think that PHP not being OO is a good thing — I LOVE OOP, and I think all languages should depend on it. This is definitely a limitation, and I’m in no way saying that PHP is the most powerful language out there — it’s far from. I’m just saying that PHP code doesn’t have to be ugly like many people give the impression of. I like to think my code is nicely written.
Sure, I could probably do it more nicely and understandably in language X, but, after all, understandability is what comments are for, isn’t it?
December 29th | #
On a semi-different note, I believe method_missing is close to or identical to PHP’s __call…
December 29th | #
Sorry for posting three times in a row (:o), but here’s just a tiny example of Python looping (which, like Ruby, definitely is an awesome ‘feature’):
dct = {’foo’:'bar’, ‘bar’:'baz’}
print ‘, ‘.join(['%s: %s' % (key, value) for key, value in dct.items()]) + ‘.’;
As you may have guessed, it just unpacks each key and value of the dictionary (hash, associative array if you will) and prints it. Granted, it looks much better than PHP code — and Ruby’s version does too.
December 29th | #
Avinash>>
I’ve heard this over and over. “It’s not fair…” I think it’s perfectly fair to be frank. If there were a decent PHP framework out there, almost every web PHP dev would be using it. As it stands now, almost every web Ruby dev uses Rails. There’s something to be said for that.
PHP’s had its time on the web. If it were possible to produce a magnificent framework, it would have been done by now. As it stands now there’s ten billion shitty frameworks out there. I’ve tried some of them (including symfony) and they cannot even be compared to Rails. It’s like comparing a slow-cooked rack of ribs to spam. This entirely stems from PHP’s language restrictions. DHH himself began writing Rails in PHP, but couldn’t do it.
Veracon>>
I think Avinash said it best. Ruby doesn’t need commenting. It’s simply that elegant. Also, I know of nothing like method_missing in PHP. If someone would like to show me otherwise, i’d be glad to see it.
December 29th | #
Anyone who understands PHP well enough will be able to read PHP code just as well as a Ruby developer can read Ruby code. To be honest, it took me a while to figure out what the names between |’s in, for instance, numbers.each{ |number| puts number }, meant.
An example of something along the lines of method_missing, http://www.veracon.net/stuff/__call.php.
January 4th | #
[...] is this Silver Ronin? Chinese Word of the Day Rails post January 4, 2006 Warpspire » Journal » Are you a Rails developer on the inside? [...]