I watched David Brady's pod call with James Edward Gray II discussing Associative Arrays. There are definitely some gems (no pun intended) in this video. While watching the video I transcribed the code examples below.
Associative Arrays with James Edward Gray II from David Brady on Vimeo.
# Associative Arrays
# http://en.wikipedia.org/wiki/Associative_array
# Ruby's 1.9 enumerator syntax, if you call an iterator and don't pass a block
# i.e. each_slice it returns an Enumerator object. Calling to_a iterates converting
# it to an array
# http://www.ruby-doc.org/core/classes/Enumerator.html
aoa = ("a".."l").each_slice(2).to_a
# Search the array of arrays to find the first element that matches
# http://www.ruby-doc.org/core/classes/Array.html#M000269
aoa.assoc("c")
# And the second element. Not the last element, see more below.
# http://www.ruby-doc.org/core/classes/Array.html#M000270
aoa.rassoc("d")
# Append elements with unshift
# http://www.ruby-doc.org/core/classes/Array.html#M000229
aoa.unshift(["i", "q"])
aoa.assoc("i") # => ["i", "q"]
aoa = ("a".."l").each_slice(3).to_a
aoa.assoc("d") # => ["d", "e", "f"]
# Note: rassoc searches the second element of the array
aoa.rassoc("f") # => nil
aoa.rassoc("e") # => ["d", "e", "f"]
# Hashes
# http://www.ruby-doc.org/core/classes/Hash.html
Hash[1, 2, 3, 4]
# Create a hash from an Associative Array
aoa = ("a".."l").each_slice(2).to_a
Hash[aoa]
# Sample was added in 1.9
# http://www.ruby-doc.org/core/classes/Array.html#M000285
words = Array.new(10) { %w[James Edward Gray II].sample }
# each_with_object added in 1.9
words.each_with_object({}) { |w, h| h[w] = true }
# Set
# http://santoro.tk/mirror/ruby-core/classes/Set.html
require 'set'
words.each_with_object(Set.new) { |w, s| s << w }
# SortedSet
# http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/SortedSet.html
s = words.each_with_object(SortedSet.new) { |w, s| s << w }
# Add to set, returns the set, otherwise if element exists returns nil
# http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html#M003478
s.add? "David" # => s
s.add? "David" # => nil
# Set operators
s.superset? Set.new(["David"])
Set.new(["David", "II"]).subset? s