From the ridiculous to the sublime: Moving from PHP to Ruby on Rails

Introduction

I’ve been a PHP developer for many years. From my earliest little scripts to our latest fully-fledged web 2.0 apps, PHP has been at the heart of it all. I can read, write, and debug PHP faster than Paris Hilton. If a client wants a new website with any kind of functionality beyond XHTML etc. then PHP it was. I could do everything I wanted, and anything I couldn’t do there’s a billion tutorials and classes on the internet.

Yet, recently its not been that great. Big apps are taking longer and longer to debug or roll out new features. Despite writing nice commented object-orientated code, I’m still re-inventing the wheel too frequently for my liking.

Now, these are two things I hate doing. Really do not enjoy. At all. Its summer, there’s a beer festival in Cambridge, and loads of great music. Working freelance, I’m paid for results, not for now long it takes me to work out why PHP didn’t do what the manual said it would.

I tried to get round this by standardising all my classes and generic server structure, and guess what? It started to look pretty much like a Rails app. Oh dear, time to think seriously. Before getting into the Ruby though, I did know of Ruby a year or two ago, but never made the switch (in truth, I took one look and left). Why?

Why PHP developers do not switch to Ruby

  1. I know PHP, I don’t know Ruby: clients need the project delivered now. This one is pretty relevant to any fundamental change. You have projects on the go, or are due soon, and you know PHP – it works. Even at the planning stage, I can usually get a very accurate estimate of the time a PHP project will take to develop. Ruby has many, many unknowns. Will it support all the things I’ve promised the client? How long will a project take? I can’t charge my clients for the extra time it takes me to learn a new language.
  2. Losing control. PHP lets me control everything; right down to the errors it gives me when it doesn’t work. I know what every part of my app is doing, because I wrote it. Ruby seems to do a lot of stuff for me – how does it know what I’m wanting to do?
  3. It seems too easy. This, I must admit, was my main reason for putting it off. Back when I first started learning PHP (and C, C++ for that matter), your first application was called ‘Hello World’; it did nothing but output the words ‘hello world’. Now with Ruby, your first app is a blog. I mean really, what’s all that about. A blog app should be hundreds of lines of code even for the basics, let alone validation, displaying and so on. Sorry, but anything that can give me a blog app out the box is like one of those Nigerian email scams…

Yet despite these hesitations, I took the leap and got stuck in. And you know what, it was magic.

There were the usual initiation woes – like the first time I switched to Mac, or the first time I played the Viola – were all the ability and creativity you had on your previous system suddenly get taken away, and you’re reduced to scrabbling around to get things working. But, these things are inevitable, so I kept going.

Good start-up tutorials

  1. Ruby on Rails Guides (http://guides.rubyonrails.org/). Fantastic overview of the basics. A lot of it you’ll get through quickly (the MVC model, for example, is simply a nice logical extension of good coding practice in any language), but it does get you immersed.
  2. Railscasts – Free Ruby on Rails Screencasts (http://railscasts.com/). This guy’s a genius. I’ve never been a huge fan of screencasts, but I have to say, he’s got a clear simple solution for all the common features you might need: authentication, e-commerce, cms etc.
  3. Learning Ruby (http://www.math.umd.edu/~dcarrera/ruby/0.3/). Rails is of course more a framework than a language; Ruby is the actual language behind it all. This is probably the biggest learn: all the new syntax and control flows. The concepts behind Rails are probably pretty clear to an experienced developer, it’s the language that’s different.

To finish then, I’ve written a list of the top five tips for PHP developers switching to Ruby. These are the points I wish I new before I started (or maybe I don’t, I certainly learnt a lot doing them the wrong way first)…

Key points for PHP developers:

  1. Rails wants you to do all the things your mother always told you. Get into the groove of a Rails App – it wants you to write DRY code, separate your models, controllers and views, write testing code, and so on. Go with it, don’t fight it.
  2. Don’t micro manage. Rails, for better or worse, takes care of a lot of stuff behind the scenes. Its not the perfect solution for every project, but if you think its right for you, then let Rails do the heavy lifting for you.
  3. Trust the syntax. I know it feels dirty and naked not ending each line with a nice ‘;’, but apparently the world doesn’t come to an end if you don’t. Write your code like natural language, then tidy it up a bit. It’ll probably work.
  4. Don’t re-invent the wheel, go for good gems. There’s a thousand brilliant gems out there to do many things you could want your app to do: restful_authentication, ActiveMerchant, acts_as_tree, and so on. Use them, and spend more time developing rather than re-inventing.
  5. Don’t kick yourself too hard… You only wasted hours of your life debugging poor PHP code.

Tags: , , , , ,

  1. Jonathan Jonathan says:

    Ruby itself isn’t the reason RoR has been so successful, it’s the MVC architecture. there are frameworks such as cakePHP and codeigniter which do the same thing for php ;)

    With this in mind, I think you’re being *slightly* unfair here. For example, you actually need to know a language before you can use an application framework written in it – so the “hello world” comparison isn’t quite right.

    The reason I’ve always been a little bit uncomfortable with Ruby is that I feel it goes a bit too far.

    Firstly, I can never get it out of my head that with every abstraction made in a language, you’re throwing another bucket of performance out of the window. I don’t think it’s really much of a surprise that the current Ruby interpreter is slow. (Although no doubt it will improve).

    Secondly, you’re right – I just don’t feel in control when using Ruby. It goes just over the top feature wise. If I want features, I’ll use/write a library, thanks. That way I’m more in control of my code – if I find a faster or better way of doing something, I can go and change it. The “Ruby way” is to be limited to one particular way of doing something hard coded into the interpreter. Which isn’t really that great, even if there are 15 alternatives :x

    Of course, not trying to say Ruby is a terrible language – I use it every now and then for quick scripts, it’s great for churning something out when you’re not too concerned about performance. But as an enterprise level solution… I dunno.

  2. Salil Salil says:

    If your goal is better programming, you will learn far more from reading high-quality code bases in your current languages than from a new language. Go read top-notch code in the languages you know already; it’ll teach you techniques and style quickly, plus different ways of thinking about problems, with the added bonus that you can actually use what you learn. You can also understand a lot about programming languages in general (issues like typing, scoping, functional vs. imperative) by reading a good book.

What do you think?

Spam Protection by WP-SpamFree