Are you organizing a Secret Santa gift exchange this holiday season? Pulling names out of a hat is sooo luddite. Jason Frey tweeted this 98-character Ruby script that will randomly pair up participants:
secret_santa.rb: names.dup.shuffle.each_with_object({}) {|n, h| h[n] = names.find {|m| m != n}; names.delete(h[n])}
— Jason Frey (@Fryguy9) November 22, 2012
Let’s try it out in a Rails console:
>> names = %w{Alice Bob Charlie David Eve} => ["Alice", "Bob", "Charlie", "David", "Eve"] >> names.dup.shuffle.each_with_object({}) {|n, h| h[n] = names.find {|m| m != n}; names.delete(h[n])} => {"Bob"=>"David", "David"=>"Alice", "Alice"=>"Bob", "Charlie"=>"Eve", "Eve"=>"Charlie"}
Someone should really turn this into a webapp.
Happy Holidays!