« So I've been doing some music... | Main | Silly Ruby 1.9 trivia »

June 10, 2008

Screencasting Ruby Metaprogramming

Tv

I've been teaching Ruby (and in particularly, metaprogramming Ruby) for almost 7 years now. And, in that time, I've gradually found ways of cutting through all the confusing stuff to the actual essentials. And when you do that, suddenly things get a lot simpler. I've always know that Ruby didn't really have class methods and singleton methods, for example, but until recently I didn't have a simple way to explain that.

Then, when preparing to give an Advanced Ruby Studio, my thinking crystalized. Metaprogramming in Ruby becomes simple to explain if you focus on four things:

  • Objects, not classes.
  • There is only one kind of method call in Ruby. The "right-then-up" rule covers everything.
  • Understanding that self can only be changed by a method call with a receiver or by a class or module definition makes it easy to keep track of what's going on when metaprogramming.
  • Knowing that Ruby keeps an internal concept of “the current class” which is where def defines its methods. Knowing what changes this makes it easier to know what's going on.

I tried this approach in a number of Studios, and refined it during some talks for RubyFools in Copenhagen and Oslo.

So Mike Clark, who's producing our new series of screencasts, started pushing me to put this description into video. Last week I finally cleared the decks enough to record the first three episodes.

First, I have to say it was a blast. I'd never recorded this many minutes of screencast before, and I was blown away by the amount of time it takes. I was also surprised at the level of detail involved, from microphone setup (which I messed up for a couple of segments) to color matching between codecs, it was fun to learn a whole new set of technologies.

I was also surprised at how hard it was to talk to a microphone. When we write books, we always try to write as if the reader was sitting there next to us. I tried to to the same approach with the screencasts, but it takes a whole new set of skills...

What I really liked was the way that I could live code examples to illustrate points. The first episode has maybe 50/50 code and exposition, and the second and third episodes are mostly code. And the code acts as a great skeleton on which to hang the concepts. Apple-R also keeps me honest.

So, if you're interested in how the Ruby object model really works, and want to improve your metaprogramming chops, why not check them out?

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/t/trackback/2226312/30017040

Listed below are links to weblogs that reference Screencasting Ruby Metaprogramming:

Comments

Thanks Dave, these are really awesome. I particularly liked the exercises you included in the third screencast; they really helped me to solidify some of the concepts. My only gripe is with your syntax coloring, but to each his own I guess. :P

I will definitely continue to purchase episodes from this screencast series as they come out.

Thanks again!

I have only had time to watch the first two and think they are amazing. The videos are packed with info, at least for someone new to this side of Ruby, and presented in a very clear manner.

I really hope there is more of these on their way.

Holy cats those first three episodes are fantastic!

I buy programming textbooks like Ismelda Marcos bought shoes. Without hesitation, those five dollar episodes are a better value than anything else in my collection. Your quality efforts really show -- these screencasts are vastly superior to any of the others in my growing collection (both from a content and fit-and-polish standpoint).

Pragprog is doing it right. Screencasts are to textbooks what presentations are to documentation. While I kinda sorta almost understood metaprogramming in ruby before, these presentations really helped to hammer it home.

I can't wait for episode 4!

Thanks!

Great screencasts!

Please add chapters to the Quicktime movies. It makes it far easier to use the screencasts as reference material.

Thanks Dave, the quality is excellent! Great material for beginners and experienced Rubyists. Eagerly awaiting your next episode :)

I have to second George's wish for chapters - that'd be great.
I'd also rather have classes named Person or something else that's a little more meaningful than Example or Dave! ;-)

Other than that - great work so far. I'm looking forward to the next set of episodes!

Another wish for quicktime chapters. That way, when I'm working on something later, I can quickly refer to the movie. Without chapters, it takes too long to scrub through the movie to find the right spot.

Hi Dave, Thanks for some very informative screen-casts. I'm not a Ruby guru and have just viewed episode one. You convinced me that: 'there is not such thing as a class method' i.e. class methods are nothing special. So..... now I wonder why I read so often that one shouldn't use class variables :)
Am I extrapolating too far in thinking class variables then shouldn't be too special either? Is this exhortation to avoid using class variables more a programming practice 'tip', or are there deeper reasons? Specifically are class variables 'resolved' in a way that is idiosyncratic , or at least in a different manner compared to instance variables? Appreciate any insights you can offer here or in a screen cast.

Mark:

Class variables lead to madness, and should be avoided (if for no other reason than their semantics changes in 1.9).

The problem is that they are basically very confusing. They are shared between classes and their instances, and sometimes shared between parents and children (in 1.8). And, they are totally unnecessary. Instead, use instance variables of classes, or use constants, or even local variables in the class body.

Thanks for the prompt response Dave! Consider then banished from my code... firing up NetBeans now :)

Post a comment

If you have a TypeKey or TypePad account, please Sign In

Now in Beta

  • Programming Ruby, 3rd Edition
    Third Edition, Covering Ruby 1.9, now in beta
My Photo

Pragmatic Stuff

Photos

  • www.flickr.com
    This is a Flickr badge showing public photos from pragdave tagged with pragdave_badge. Make your own badge here.

Site Search

  • Google Search

    The web
    PragDave