the all-thing | 2010-09-06 19:13:03 -0400 ========================================== A ruby puzzle ------------- Date: June 12, 2008 8:39pm Author: William Morgan Labels: ruby URL: http://all-thing.net/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 24, 2008 9:42pm: ["| Well ...?\n", "| \n"] grant, on June 25, 2008 12:47pm: ["| @uniq_by@?\n", "| \n"] William, on June 25, 2008 3: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 2:39am: ["| @uniq_by@. yes! i swear i thought of that before visiting the comments page.\n"] This delicious text version served up by Whisper .