Learning Ruby: Let Your Curiosity Be Your Guide

by doug on January 24, 2010

in Editorial

?!

It’s the first week of the semester for my online Ruby course. One of the first meta lessons I try to teach is to pull on the thread. I’m thinking of a loose thread on a knitted sweater. You have an itch to pull on that thread, knowing the sweater will unravel; if you pull long enough and patiently enough, you’ll end up with the other end of the thread in your hand.

Everyone in the class wants to know Ruby. Wanting to learn will carry you to the first step, and the second. However, if you want to really learn Ruby, you’ve got to be curious about it because curiosity carries you through to the end of the thread.

Today, a student asked me a question I didn’t know the answer to. When this happens in the classroom, I can say “I don’t know. Your assignment is to dig up that answer and tell us about it next class meeting.” In 9 years of teaching, only 3 students have been energetic enough to take up my challenge, but I always go home and look up the answer for myself.

An online course is different. I can dig up the answer. Here’s the question I got today:

I have two installations of Ruby: 1.8.7 on a laptop and 1.9.1 on a desktop, both Windows. In my 1.8.7 install I can do this:

    y 99.methods.sort

This y command does something like take in an array and output YAML. Is there a require that I have to do or a gem that I have to download to use this?

I can tell that this student is already committed to learning Ruby and already knows about gems and require. Here’s my answer.

This is a really interesting question. To answer it we need to talk about Modules and require, and how Modules can be used in classes. We’ll be talking about this subject later in the course, so if you (the class in general) feel mystified at this point, fret not. We’ll be covering this idea later.

However, for those who are interested, I’ll go a little further into it.

That ‘y’ method comes into irb when the Yaml module is “required”. irb does not do this by default, so I would guess that your particular version of irb is configured to ‘require’ Yaml. (None of my installations of Ruby on my Mac, PC, etc do.) It turns out that different implementations of Ruby, on different systems, configure irb differently. Your 1.8.7 version happens to ‘require’ Yaml.

If you want to use ‘y’ in irb, you can do this do this at the irb prompt:

require 'yaml'

If you are really curious you can to this.

1) Start irb afresh and do:

self.private_methods.sort

2) Visually scan the list of methods to get an idea of what’s in the environment.

3) Do this:

require 'yaml'

4) Run this code again:

self.private_methods.sort

5) Notice that methods have been imported from the Yaml module, and a new method called ‘y’ appears in the list.

6) You may think that the y method is in the Yaml module, but being the curious soul you are, you decide not to make unwarranted assumptions. Instead, you decide to dig into the source code. This a beautiful thing about open source languages: you have the source code to answer all questions about how the language implements its “magic”.

7) Next you do a search on your system (PC, Mac, Linux doesn’t matter) and look for a file named ‘yaml.rb’, which is the file that Ruby will look for when you say “require ‘yaml’”
8) Once you find ‘yaml.rb’ and look at the code, you finally know where ‘y’ came from.

Of course, along the way you may encounter mysteries and more questions will flood into your mind. But, this is a good thing. Ruby is deep and subtle. And fun.

You get the idea. I encourage digging through the source code. It’s a great way to see how Ruby code is really written. When you have technical questions that go beyond the scope of the assignment, beyond what “Beginning Ruby” offers, beyond my knowledge and experience, you can always go to the source code itself.

In this case, the file you’re looking for will be someplace like this (Windows users will have to look in C:\Ruby):

/usr/lib/ruby/1.8/yaml.rb

Have fun.

And of course, happy hacking…

Share and Bookmark
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • FriendFeed
  • StumbleUpon
  • Tumblr
  • Twitter
  • Add to favorites
  • LinkedIn
  • Posterous
  • Reddit
  • Suggest to Techmeme via Twitter

Leave a Comment

Previous post:

Next post: