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
My name is Evan Light and, yes, I am a nerd. I'm also a professional software developer who, after spending one too many years contracting to the federal government, escaped into the far more enjoyable commercial world. Having spent several years using C and even more using Java (the latter very nearly caused me to give up programming entirely), I consider myself fortunate to have discovered Ruby and to use it as part of my daily work.