What is Toluu?
Toluu is a free service for sharing the feeds you read and discovering new ones.
Get Invite

codefluency


More on super-and-extendToday

A few days ago I posted an article on method wrapping, and I thought I'd touch a bit more on the super-and-extend technique; I've had a few questions on it that warrant some clarification.

Multiple Wrappers

Yes, the technique works with multiple wrappers on a single class; when the class is extended with additional modules, they stack and super works as expected (wrappers are applied on the "outside," so invocations occur in LIFO order).

You can see a full example of this as a Gist. Here is a sample irb session showing that the multiple wrappers are applied correctly:

>> (class [BazWrapper, BarWrapper, FooWrapper, Class, Module, Object, Kernel] >> widget = Widget.new >> (class [BazWrapper::WrappingB, BazWrapper::WrappingA, BarWrapper::Wrapping, FooWrapper::Wrapping, Widget, Object, Kernel] >> widget.render_on(nil) BazWrapper::WrappingB BazWrapper::WrappingA BarWrapper::Wrapping FooWrapper::Wrapping Original

Wrapped Class Inheritance

If you wrap a class, you wrap inherited classes as well, regardless of whether the classes were defined before or after the point the wrapping is applied.

Given the following class definitions and placement of the wrapping, all three classes would be wrapped as expected.

class Widget end class WidgetSubclassDefinedBeforeWrapping
Wrapping a Method in RubyJanuary 3

Let's say you have a Ruby class with a method you'd like to wrap—for debugging or performance timing—and, since you don't control where the class is instantiated (think overriding a method in Rails' ActionPack), just creating a subclass and using super isn't going to work.

Let's take a look at two mixin patterns; one a ubiquitous naming hack and one a bit of esoteric Ruby inheritance trickery.

First, let's set the scene. Let's say we have a method, Widget#render_on:

class Widget # Wrap this one def render_on(document, options = {}) # ... end # ... end

What we want is for each wrapper we write to call a method, which we'll call snapshot, that will output some information about the method invocation. Don't worry about the implementation too much here; it just takes a block and times the yield:

def snapshot start = Time.now result = yield # You could store these values somewhere # for averaging, track the caller, etc puts Time.now - start end

So, we're going to do this two ways…

Technique #1: alias_method_chain

ActiveSupport makes this easy. Though arguably a bit of a messy naming hack, it does the job and is a bit self-documenting, forcing you to name a feature. If you're using Rails, this is happening all over the place.

module AliasMethodChainWrapper def self.included(base) base.send(:include, InstanceMethods) base.alias_method_chain :rende
Revisiting PythonDecember 26 2008

I used to be an avid Pythonista. I poured over Python books, read huge swathes of Python code (including the source of Twisted, which, as its name might imply, I think of as quite a personal accomplishment), and soaked up as much material as I could find. I had survived the relative chaos of Perl, and was relieved to find a more orderly, seemingly sane language to work with. I dove in, and I loved it.

I learned a huge amount during those years; Python was my first OOP language, and cemented what has become my obsession with dynamic, “lightweight” languages. I’ve never studied CS formally (having opted for languages and linguistics instead) so Python’s more draconian rules and academic bent helped temper my more artistic tendencies and pushed me far enough into the land of algorithms, data structures, and big O notation so not to be seen as a complete dilettante by later CS nerd colleagues.

One day, I stumbled across a lone copy of Programming Ruby by chance at a bookstore (back when you just didn’t find Ruby books). At the time, I thought of Ruby as merely “a Japanese Perl,” so I only picked it up on a whim — but only a few chapters in, I was completely hooked.

Although I continued to use Python (and PHP) professionally for the next couple of years, and very successfully — I spent my spare time on Ruby, and became very active in the community. In 2004 I fully switc

Front Row to HistoryNovember 9 2008

I had to tell this story several dozen times at RubyConf, so I thought I’d wrap it up for posterity.

It all started with an email. The campaign was running a contest entitled “Front Row to History,” and would select
5 first-time and 5 repeat donators to attend the rally in Chicago— what would later become a massive victory celebration.

I had donated to the campaign before and had, like many of you, become more and more fervent in my support of Obama as election day approached. I’m an independent voter, but my support was won after researching policies and watching the tone of the campaigns as they progressed.

So, I did a bit of early voting with my wallet. Again.

The Barack blog calleth

Halloween night was fantastic. I spent the evening with my wife, Melissa, and sons Braedyn and Jamis (Peter Pan and Captain Hook, respectively) in the Hyde Park neighborhood in Austin, guests of my generous boss, Steve Sanderson, and his lovely family. We had a blast; with well over a thousand kids and blocked-off roads, this was the place to be.

Then, unexpectedly, the Barack Obama blog called, wanting an interview. They had read the little blurb I had added on the donation form as a cathartic gesture, detailing my support in light of the fact I was an Arabic Linguist in the USAF for 6 years, before 9/11 and through the first couple years of the war. As they ex


Boston.rb SlidesSeptember 9 2008

The material for the talk is fairly similar to the Lone Star 1.9 talk I did a few days prior, but re-tooled for a smaller group.

Thanks to my employer, FiveRuns, for bankrolling the trip, and to the guys out at Thoughtbot and the Boston.rb group for their hospitality.

New in Ruby 1 9Upload a Document to Scribd
Read this document on Scribd: New in Ruby 1 9