From: Martin DeMello Date: 2010-04-09T23:46:51+09:00 Subject: Re: read and write stock prices to file using arrays On Fri, Apr 9, 2010 at 7:27 PM, Peter Miller wrote: > > I didn't realize a hash could point to more than one object. Awesome. It can't, but the one value a key points to can be an object of any type, including arrays etc.. For instance, a very common ruby idiom is to group a set of objects by using a hash of arrays. For example, let's say you have a bunch of people, who belong to one of three teams, red, blue or green. data = [['john', 'red'], ['jane', 'green'], ['alice', 'red'], ['bob', 'blue'], ['tom', 'red'], ['dick', 'green'], ['harry', 'red'], ['bill', 'blue']] You can collect the teams like so: teams = {} data.each {|name, team| teams[team] ||= [] # initialize with an empty array if this is the first time you've seen the team teams[team] << name # append the name to the team's array } p teams # => {"red"=>["john", "alice", "tom", "harry"], "green"=>["jane", "dick"], "blue"=>["bob", "bill"]} martin