From: Robert Klemme Date: 2010-08-20T20:07:54+09:00 Subject: Re: Need help with hash.to_s for enumerated items 2010/8/20 Sem Ptiri : > I have a grid with keys > > grid.keys > => [[1, 2], [0, 0], [3, 3], [2, 1], [1, 1], [0, 3], [3, 2], [2, 0], [1, > 0], [0, 2], [3, 1], [2, 3], [4, 3], [3, 0], [2, 2], [1, 3], [0, 1], [6, > 3]] > > I need to create a list list of all possible combinations. Combinations of what? Combinations of all Arrays that are your keys or of all elements in those arrays or...? > I would start > by taking the first item, then linking it with every other item, then > truncate the first item, and repeat. "Truncate"? > My problem is that on > > grid.keys[0].to_s > => "12" > > it mashes the [1, 2] together making it unusable. I have no idea what Array#to_s has to do in combining elements. > Could you suggest a better data structure for storing a list of all > possible combinations? Why not use an Array of Arrays? > Linking a hash [1, 2] to [0, 0] and then [1, 1] would simply override > the previous link. What exactly do you mean by "linking"? Are you talking about replacing a hash key? Or are you talking about using a Hash key as value of another key? As soon as you set another value for the same Hash key the old value is gone: irb(main):003:0> h={} => {} irb(main):004:0> h[[1,2]]=[0,0] => [0, 0] irb(main):005:0> h => {[1, 2]=>[0, 0]} irb(main):006:0> h[[1,2]]=[1,1] => [1, 1] irb(main):007:0> h => {[1, 2]=>[1, 1]} Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/