Posts Tagged: Rails

(Finally) Learning Ruby on Rails: Month 1

I’m trying to make 2012 the year I finally learn to code. Not HTML/CSS/jQuery, but actual code—you know, if statements, and things like that.

So just a few days after Christmas, I chained myself to my desk and cracked open my worn copy of PHP/MySQL Development and a Lynda.com essential training course, and started in. I don’t think I lasted more than a few hours that first afternoon. Its not that I don’t get PHP, I think I just can’t stand it. I distinctly remember where it all went off the rails. See the screenshot below.

What a complete mess. Sure, I can read it, and for the most part I know what it says, but it does require some mental energy to just process and decode the structure of it. It just isn’t welcoming. My first instinct when I look at code like this is to sigh at the pain of having to mentally organize it before I can figure out what its doing. That’s aside from the fact that all you can really see are < and > signs, question marks, brackets and semicolons.

I’ve known this about PHP for a long time. I’ve struggled with getting beyond the beginner’s stage with it at least a half-dozen times in the past few years. I always get fatigued, normally stop at the same place, and then get too busy  and move on to something else after a few days. I’ve just never been able to find any traction, never been able to really dig in.

Enter Ruby, Again

I say again, because this certainly isn’t my first brush with Ruby either. I’d done a few tutorials and done the magical TryRuby thing, and thought it was really cool. But I’ve gotten hung up on the complexity of Rails more than once. And don’t let anyone tell you it isn’t complex. When you dive into Ruby and Rails you get it all thrown at you at the same time: object oriented programming, an MVC framework, Ruby gems and their dependencies, Rails ‘magic’, etc etc.

It’s quite a lot to get your head around—and until you have at least a tiny, tiny amount of knowledge about each, you’ll never get anywhere. Ruby is 100% object oriented (still figuring that out), Rails lives and breathes MVC (a paradigm I’d never used before), and figuring out gem bundle install errors and dependencies will make you old long before your time. In short: its tough.

But this time, Ruby’s simplicity, elegance and readability was always there, pulling me along the whole time.  Just have a look at this excerpt from a program that fetches and downloads webpages from Wikipedia, from the fantastic Bastards Book of Ruby:

require "open-uri"

remote_base_url = "http://en.wikipedia.org/wiki"
remote_page_name = "Ada_Lovelace"
remote_full_url = remote_base_url + "/" + remote_page_name

puts "Downloading from: " + remote_full_url

remote_data = open(remote_full_url).read

my_local_filename = "my_copy_of-" + remote_page_name + ".html"

puts "Writing to: " + my_local_filename

my_local_file = open(my_local_filename, "w")
my_local_file.write(remote_data)
my_local_file.close

It is long, and probably doesn’t make sense to you, but just try reading it line by line. It basically sets a base url (the base Wikipedia url), adds the name of the page we want to fetch (the entry for Ada Lovelace), downloads the file and then pastes it into a file on our harddrive. Again, its a bit dense, but you can probably get the gist of what its trying to do, even if the syntax doesn’t make sense.

After running through a few more of the tutorials from the Bastards Book of Ruby, I was hooked. The code was readable, none of it seemed impenetrable, and I seemed to be able to write code myself (or at least predict how it was going to be written) fairly quickly—within a day or so. I was far from functional without a tutorial to follow, but I was able to get a basic understanding of what the code was doing, without getting frustrated by the syntax.

It’s about Momentum

After a good, solid Saturday and Sunday running through tutorials and watching screencasts, I was really starting to get interested. That was the strange part for me: in numerous attempts to try and learn PHP, I never once felt that urge to go home and open up a new tutorial. I always had that nagging feeling like it was going to be an onerous, complicated chore. I always ploughed on with a no-pain-no-gain mentality, but I never, ever felt excited about learning—and within a week or so, I’d just give up. With Ruby, I already feel like I have some actual basics to work with now, and more importantly, I’m still excited about learning after four weeks. I actually feel like I have some momentum to keep going with—that mixture of excitement, interest and slow-and-steady accumulation of actual knowledge.

Goals for the first month

My goal for the first month was simple: just watch and read as much as I possibly could.

Instead of learning nuts and bolts, and trying to actually write and memorize a lot of code and syntax, my goal was to just see as much as I could. I hoped that if I kept looking at new material, I’d keep seeing similar things over and over.

This worked out quite well. In the 25+ tutorials and articles I read, for example, almost all of them used loops in some way. After all this reading, I was able to pick out which part was the loop, and really see how it worked and how to write the syntax for it.  Same with if statements (known as part of ‘flow control’). Just reading and re-reading if statements really ingrained them in my head. After the first week or two, when I actually started to write some code of my own, I already felt a bit more comfortable.

Resources from Month 1:

  1. Lynda.com Ruby Essential Training-> got through about 50%
  2. Lynda.com Ruby on Rails Essential Training -> got through about 60%
  3. Bastards Book of Ruby -> Maybe 30% of this, but I also ran through lots of the code samples
  4. Rails Tutorial -> About 50% done, still working on this one
  5. Rails for Zombies -> This one seemed silly, but is actually very, very good
  6. Several short Sinatra tutorials

If I were starting again, I would probably do them in that order. Learn a little bit of Ruby, get totally mind-boggled with the Ruby on Rails Essential Training, go back to Ruby again briefly (it should make much more sense now), then move on to Rails Tutorial. Rails Tutorial is really the gold standard tutorial out there. Its long, detailed and brings you right from start to finish. You do get lost at times, but its the best resource I’ve seen out there to date.

Next Steps

This month, I’m planning to finish the Rails Tutorial (building a Twitter clone), and I plan to actually try and build something simple from scratch. My plan here is to build something like a recipe catalog or an image gallery or something small like that to get the hang of things. My intention is to ‘scaffold’ (auto-generate) half of it and try actually writing the other half. Maybe I’ll scaffold a Users resource, while actually coding a Recipes resource…something like that.

All in all I think month 1 was reasonably successful. I still can’t really code things without guidance in Ruby, and I only really have a loose handle on just the V part of MVC (Views) in Rails, but things are moving along already.

For completeness, here’s a quick inventory of what I do have some idea about in Rails:

  1. I have some clue now about what the whole REST thing is about
  2. Models are where the database parts go
  3. Controllers usually (?) start with a few basic methods: index, new, show, edit, delete
  4. All of these methods have their own template/view files
  5. URLs get setup in the routes file—each method has its own url, and you can manually define your own, including a “root” route or homepage

And that sounds and feels like progress to me! Stay tuned for next month’s update.