I didn't know there was a Method#to_proc !

Learned a clever trick while perusing the innards of Turbulence.

Symbol#to_proc, as you may know, is a clever hack that has been around for some time. However, Chad, (I presume–he hacked Turbulence together with Corey Haines and Michael Feathers), does something cute.

CALCULATORS.each(&method(:calculate_metrics_with))

I’m left wondering if that’s doing what I think it does. So I whip out VIM:

class Foo
end

def bar(blech)
  puts blech
end

[Foo.new, Foo.new].each(&method(:bar))

=> [#<Foo:0x103462608>, #<Foo:0x1034564c0>]

Cute!

Whereas Symbol#toproc would’ve called #blech on Foo, instead, we’re using Method#toproc to invoke #bar on self. I didn’t even know Method#to_proc existed. Surprise, surprise!

Posted by evan on Saturday, March 05, 2011

blog comments powered by Disqus