March 2011 Archives

Fiddling with Redis sorting

Nick Quaranto’s excellent Redis talk at Mountain West Ruby Conference particularly captured my fancy when he demonstrated Redis sort. An audience member asked whether it was possible to chain the result of gets on a sort. Redis’ documentation is excellent; however, the entry on multiple gets in a sort was just a touch unclear.

During a break today at MWRC, I through together this quick example. I’m also posting it here as someone on Twitter questioned my conclusions (as I do myself). Provided below:

Posted by evan on Mar 17, 2011

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 Mar 05, 2011