Skip to content

Instantly share code, notes, and snippets.

@timuruski
Created February 8, 2012 23:26
Show Gist options
  • Select an option

  • Save timuruski/1775382 to your computer and use it in GitHub Desktop.

Select an option

Save timuruski/1775382 to your computer and use it in GitHub Desktop.
# As illustrated in http://confreaks.net/videos/427-rubyconf2010-zomg-why-is-this-code-so-slow
Person = Struct.new(:name)
people = [Person.new('steve'), Person.new('bill'), Person.new('larry')]
puts people.inject({}) { |table,person| table[person.name] = person; table }
puts Hash[people.map { |person| [person.name, person] }]
# {"steve"=>#<struct Person name="steve">, "bill"=>#<struct Person name="bill">, "larry"=>#<struct Person name="larry">}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment