![]() |
Articles Feed |
Categories
Archives
- July 2010 (5)
- June 2010 (4)
- April 2010 (3)
- March 2010 (2)
- February 2010 (2)
- January 2010 (1)
- December 2009 (1)
- October 2009 (2)
- September 2009 (2)
- August 2009 (1)
- July 2009 (5)
- June 2009 (2)
- May 2009 (2)
- April 2009 (8)
- March 2009 (7)
- January 2009 (2)
- December 2008 (3)
- November 2008 (5)
- October 2008 (4)
- September 2008 (6)
- August 2008 (4)
- July 2008 (5)
- June 2008 (5)
- May 2008 (4)
- April 2008 (2)
- February 2008 (4)
- January 2008 (2)
- December 2007 (2)
- November 2007 (2)
- October 2007 (2)
- September 2007 (1)
- August 2007 (3)
- July 2007 (1)
- June 2007 (4)
- May 2007 (7)
- April 2007 (2)
- February 2007 (3)
- January 2007 (3)
- November 2006 (3)
- October 2006 (3)
- September 2006 (17)
- November 2004 (1)
Meta-programming Comes Naturally to Intern
by: doug | June 22nd, 2009 |
We have a couple of interns in the office this summer. They are kind of like Jr. Apprentices - distinguished mostly by the fact that they are in school and will be returning to school in the fall. I've been working with Andrew this summer. He just finished his Sophomore year of High School and is just learning to program. He's been working through a couple of ruby tutorials: Chris Pine's Learn to Program in Ruby and the ever-so-entertaining-especially-for-a-high-schooler Why's Poignant Guide to Ruby.
He was working on the Orange Tree example for Pine's tutorial. He had the class written and want to write a command line interface to it. This was what he tried to do.
- tree = OrangeTree.new
- command = gets
- tree.command
"What are you trying to do Andrew?"
"I want to get a command from the user and call that command on the tree object."
Now I had expected that Andrew would write a big if-else chain comparing strings and calling methods. In my mind that's what a beginning programmer should learn. I learned to program in C++ and that's the way I had to do it! I wasn't even sure that we would even touch meta-programming this summer.
But I had a second thought. "Why not show him some Meta Programming now?" I don't want to be responsible for destroying the future career of an aspiring programmer, but what the heck? How much damage could it possibly do?
So I explained to Andrew how methods in Ruby aren't really methods, they are messages and how you can "send" a message to an object. We modified his program to do what he wanted.
- tree = OrangeTree.new
- command = gets.chomp.to_sym
- tree.send(command) if tree.responds_to?(command)
I'm not sure that he completely understood all that, but there it is. Ruby let Andrew do the thing that he naturally wanted to do.

June 24th, 2009 at 01:26 PM
That is quite elegant.
July 15th, 2009 at 04:18 AM
Great! Maybe we should pay more attention to how juniors try to program instinctively - could it prove fruitful for language design? I remember making similar kinds of "mistakes" to Andrew's first attempt when I was first starting out with programming... but of course I'd never make that kind of mistake now because I'm old and cynical and know better. Perhaps we lose something as we become experienced programmers?
July 16th, 2009 at 07:41 PM
Great, now lets hope the method doesn't have any arguments and isn't never overridden. Or can ruby handle that too? Seems like it might due to dynamic typing.
July 28th, 2009 at 05:55 PM
Andrew has a perfect understanding of the semantics. He just didn't quite grok details.
C++, Java, etc., would punish him for not molding his thought process to the language straight-jacket.