william's blog | 2012-02-04 12:21:34 +0000 =========================================== A ruby puzzle ------------- Date: June 13, 2008 12:39am Author: William Morgan Labels: ruby URL: http://masanjin.net/blog/a-ruby-puzzle.txt Name this function: inject({}) { |h, o| h[yield(o)] = o; h }.values Hints: 1. It's a variant of a common stdlib function. 2. The name has 7 characters, one of which is an underscore. A survey of my rubyist colleagues suggests this is a hard question. Much harder than writing the function given the name, which took about 10 seconds. Replies -------- Anonymous, on June 25, 2008 1:42am: ["| Well ...?\n", "| \n"] grant, on June 25, 2008 4:47pm: ["| @uniq_by@?\n", "| \n"] William, on June 25, 2008 7:23pm: ["| @uniq_by@ is correct! Very nice. Of course it doesn't preserve the order of\n", "| things, which Ruby's uniq does. (But whether that's a feature of uniq or just\n", "| a happy coincidence that might change in the future, I don't know.)\n", "| \n"] Brendan, on July 5, 2008 6:39am: ["| @uniq_by@. yes! i swear i thought of that before visiting the comments page.\n"] This delicious text version served up by Whisper .