Posts tagged as:

Learning

?!

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…

0 comments

(Reading time 5 minutes)

The common wisdom is that our elders are wiser than we are.

My old man gave advice to anyone who would listen. His favorite piece was, “Become a CPA.” That’s what he always wanted to be. When he was 40-years old (and I was 18) he looked me dead in the eye and said, “Life has passed me by. But you’ve still got a chance to be a CPA.”

The problem was that I wanted to be a Beatnik novelist, wear a beret, and sit in coffee shops in North Beach drinking coffee with girls with long black hair. Of course, there was jazz and poetry in the background. And we would be ride about town on my Vespa motor scooter.

Ignoring the my old man’s advice, I jumped out of the Physics/Math fast track and headed down the rocky path to Comparative Literature. I figured that I was pretty good at math, so writing novels wouldn’t be much of a challenge. I would become a novelist, be rich and famous, and loaf around in coffee shops.

The problem was that I was a mediocre writer. I could write a few sentences that showed promise, but I was lazy and didn’t work at it much. I expected it to come to me easily, the way the math did. Writing fiction turned out to be more work than I anticipated. I dabbled now and then, and every rejection notice was a nail driven into my ego.

In the early 90′s I thought about changing careers, so I took up programming. I took a few courses and the local community college and I was good at it. Programming was easy for me, but I found it boring and tedious. Who in their right mind would want to write financial software for insurance companies? Not me. I wasn’t born to toil in that salt mine.

My loathing of programming inspired me to go back to fiction. I wrote a novel about my 2nd marriage. My first marriage had been a tragedy, and the 2nd had been a catastrophe that was clearly a great topic for a gritty novel. The book spilled out of me and was finished in 9 months. I felt that it was good enough to show to my friends. Big mistake.

It turns out the book had a big problem—it dealt with my stupid, carnal, hormonal side. It was about lust, sex, and bad judgment—all of the reptilian instincts were at play. I didn’t mind that the book put me in a terrible light because it was actually a work of Art. So, I took a chance and showed the book to selected friends—educated readers who could handle my Rabelasian adventures. I expected them to read the book in the spirit that they would read Henry Miller or Charles Bukowski.

Wrong again. Every reader handed manuscript back as if I had asked them to fondle a soiled pair of underwear.

“How could you write that sick shit?” said one woman.

“It all happened to me,” I said. “If Bukowski had written it, you would love it.”

“Bukowski’s an artist. Don’t you see that this is pornographic? I’m shocked,” she said.

She hasn’t talked to me since.

Several years later I sent the novel to an agent. “We don’t deal with that raw stuff. It’s not commercial,” he wrote. “You could take out some of the big words and send it to Hustler.”

I put the novel on the shelf, where it’s been ripening for the last 5 years. Now and then I take it out and read it. I enjoy it. It’s disturbing, but satisfying to read it. It’s a special book, about me, written just for my eyes. Everyone should have their own novel.

Now that my father is gone, I think about him and his advice. I would never be a CPA. Then again, I didn’t end up in North Beach, except for an occasional visit to City Lights. I don’t spend my time in coffee shops with Bohemian literati, but I go to Starbucks and Peets quite a lot. I don’t ride Vespa, either. The truth is, I’ve gone back to math and science. It’s like going back to my real home. I now take great pleasure in programming and teaching.

None of my students know, but underneath the mild-mannered software engineer lecturing about maintaining state across HTTP requests, I am a novelist…a secret novelist, who happens to be a hell of a programmer and teacher.

0 comments