Archive for the ‘rails’ Category

Learning to read

Monday, August 18th, 2008
Image licensed under Creative Commons

Image licensed under Creative Commons

No, this is not a post about illiteracy.

OK, now that I got that off my shoulders…

I’ve been learning Ruby on Rails for the past few weeks, and I’ve encountered an interesting question that I’ve faced a few times before and never really found a good answer to.

How do you read code?

And I don’t mean that in a metaphysical sense. I mean when you’re learning a new framework, language or technology you usually look at some code written in it to try and see how real world applications use it. These days you can pretty easily find Open Source code written in almost anything. What I find difficult is figuring how to read the code of these applications.

I’ve read a book about Ruby on Rails, so I understand the basics decently well. I’ve created a toy application just to see how to do it. But the jump from that to digging through source code for a full fledged real application is pretty huge. Where do you start? Initally I started looking at the models to try and understand the objects that the application dealt with. But I found that to be a bit confusing. Then I looked at some of the key controllers (like user authentication and those that handle the main pages) and also found that a bit confusing.

Finally I sort of settled on looking at a specific page in the app, looking through the view that generated it, then going to the controller that was invoked, and maybe looking at some helper methods and model code that I found there. I found that to be a reasonably understandable strategy. But I’m not really sure if it would have worked as well if I didn’t already have looked at some model/controller code before hand.

This is not specific to RoR. If you learn a new development environment, how do you figure out where to learn the code?

Running ‘ri’ with colors under Mac OS X

Tuesday, July 29th, 2008

Ruby has a command line utility called ri for looking up documentation, and ri has a nice feature of displaying its output with pretty colors. Unfortunately, when I tried to run it (ri -f ansi) what I got was:

After tinkering about a bit I gave up. But today I decided that I’ll have my damned pretty colors even if I have to spend a lifetime getting them! Thankfully it only took 10 minutes. ri uses less to display its output in a scrollable form, if you make it not use less you get what you want by giving it the -T option:

“But…but…but… I like scrollable output!”, says you, whimpering softly.

Well, stop your cryin’, so do I.

less itself takes an option to treat escape characters as escape characters (duh!) instead of just printing them. So you want to make it use it. Here’s what you want to add to your Bash profile file to make the magic happen:

# I likes pretty colors!!!1!!
export RI="-f ansi"
export LESS="-R"

And the result is: