10 Reasons why Ruby is better than PHP

I bet you thought I'd never get any more reasons out did ya? Well, today, I'm going to tell you all about IRB and the Rails command line debugger, both of which I use religiously.
One of the great pleasures of using Ruby, apart from the previous three reasons, and of course the following five reasons, is the wonder that is the Interactive Ruby console, or IRB. IRB is an interactive interpreter; which means that instead of processing a file, it processes what you type in during a session. It's a great tool for testing Ruby code, and a great tool for learning Ruby.
Once you have Ruby installed on your computer, you will notice that the ruby command will do the same thing as PHP - it silently waits for a script to be streamed in on stdin. On it's own, that's not particularly friendly, or even that useful. But another command that is installed alongside Ruby, is the irb command. Start up your command line tool of choice, and simply enter these three immortal letters: irb. Then you will see this:
This is the irb prompt. Now all you do is simply type any ruby code, and it will show you the result of each and every expression as it is evaluated. Not sure how a method works, or can't remember the output of a method? Then test it with IRB.
IRB lets you do things without having to fake out your controller. For example, in PHP, I'm forever using echo or CakePHP's debug function to print out some useful info to the browser when debugging my code. But when developing with Ruby, I simply have my trusty IRB prompt ready and waiting for me to inflict world domination.
Each of the above lines will be following by IRB echoing the result. So I can see exactly what is going on, and without me having to echo out data all through my code and to the browser.
This is actually very, very useful, and is a great way to test your code, or in fact any code at all. It's even better if you are a command line freak like me.
What is even better, better (!?) is that Rails also has its own command line interpreter, which is basically an extension of IRB. What makes it a little different, is that when it is started up, it also loads all your Rails code. So you have instant access to your Rails models, which is also very handy. Just cd to your Rails app root, and run:
You are then shown a similar command line prompt to IRB, and can run any part of your Rails app code.
To wrap up reason #4, I want to tell you about one of the best things about developing with Ruby on Rails. It's another extension of IRB, similar to Rails script/console, but is simply genius when debugging your Rails code.
There are several ways in which you can run your Rails app, but the traditional method is by using the the script/server script within your app. When run on the command line, this will start up a small web server, usually run by Webrick or Mongrel. Now I wouldn't recommend this method when running your app in production, as there are much better, and more efficient ways to do so, but I use this all the time when developing or testing my app code.
Just cd to your Rails app root and run this on the command line:
Notice I appended a command line flag; -u. This tells the Rails server to start up in Debug mode. If you use Mongrel, this is what you will see:
And the command line prompt will sit there blinking at you, waiting for you to load your app in your browser. So don't keep it waiting, open up your browser, and go to http://localhost:3000, and you will be shown your Rails app in all its glory.
Now we get to the good bit. Open up the code of any of your controllers in your favourite IDE or text editor. Then type the word debugger within one of your actions. Then go back your browser and navigate to that page. You should the notice that the page does not appear to finish loading. That is because your debugger keyword within the action method, has paused the server until you tell it to continue.
Go back to your command line when you started the Rails server, and you will see a bunch of log data, following by what looks like an IRB prompt. That is effectively what it is. You can now type any Ruby code at that prompt, just like you would with IRB or script/console. But the beauty of this, is that you have access to all the variables and methods at exactly the same point in your code where you placed the debugger keyword.
So instead of having to type echo or print statements throughout your code, and running them through your browser to see the results. You can add the debugger keywork at the point where you want to debug, and use the command line to access your code in real time.
I really hope you got all that, as this feature has saved me hours and hours of time when debugging my code. I can honestly say that it is a life saver. You really should try it out now, or if you're a little impatient, try it out in your browser for instant gratification. You should also check out Amy Hoy's "Secrets of the Rails Console Ninja's".
Thanks again for sticking with me, and hold on to your pants for reason #5 where I will be talking about Modules; Ruby's answer to name spaces. Which will lead me nicely onto another Ruby exclusive: Mixins!
P.S. I really appreciate everyone's comments. Keep them coming.

No comments:

Post a Comment

Please Provide your feedback here