# Need help with hash.to\_s for enumerated items

**URL:** <https://www.ruby-forum.com/t/need-help-with-hash-to-s-for-enumerated-items/193601>\
**Category:** Ruby\
**Created:** [August 20, 2010, 11:03am UTC](https://www.ruby-forum.com/t/need-help-with-hash-to-s-for-enumerated-items/193601 "2010-08-20T11:03:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sempti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sempti](https://www.ruby-forum.com/u/sempti)\
**Post date:** [August 20, 2010, 11:03am UTC](https://www.ruby-forum.com/t/need-help-with-hash-to-s-for-enumerated-items/193601/1 "2010-08-20T11:03:52Z")

</div>

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. I would start  
by taking the first item, then linking it with every other item, then  
truncate the first item, and repeat.

My problem is that on

grid.keys[0].to\_s  
=\> “12”

it mashes the [1, 2] together making it unusable.

Could you suggest a better data structure for storing a list of all  
possible combinations?

Linking a hash [1, 2] to [0, 0] and then [1, 1] would simply override  
the previous link.

---

<div class="post-metadata">

**Author:** ![sempti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sempti](https://www.ruby-forum.com/u/sempti)\
**Post date:** [August 20, 2010, 11:05am UTC](https://www.ruby-forum.com/t/need-help-with-hash-to-s-for-enumerated-items/193601/2 "2010-08-20T11:05:48Z")

</div>

Would it be a better option to create a custom class for linking?

---

<div class="post-metadata">

**Author:** ![sempti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sempti](https://www.ruby-forum.com/u/sempti)\
**Post date:** [August 20, 2010, 1:08pm UTC](https://www.ruby-forum.com/t/need-help-with-hash-to-s-for-enumerated-items/193601/3 "2010-08-20T13:08:28Z")

</div>

2010/8/20 Sem P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![sempti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sempti](https://www.ruby-forum.com/u/sempti)\
**Post date:** [August 20, 2010, 5:31pm UTC](https://www.ruby-forum.com/t/need-help-with-hash-to-s-for-enumerated-items/193601/4 "2010-08-20T17:31:08Z")

</div>

Sem P. wrote:

> 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. I would start  
> by taking the first item, then linking it with every other item, then  
> truncate the first item, and repeat.
> 
> My problem is that on
> 
> grid.keys[0].to\_s  
> =\> “12”
> 
> it mashes the [1, 2] together making it unusable.

On my system array#to\_s joins the arrays’ elements (“12”)  
arrai#inspect prints the array [1, 2]

> Could you suggest a better data structure for storing a list of all  
> possible combinations?
> 
> Linking a hash [1, 2] to [0, 0] and then [1, 1] would simply override  
> the previous link.

If I understand correctly the problem, you can:  
1 - assign the keys to an array varable, say gk=grid.keys  
2 - create a list of indexes for each possible pair in gk  
3 - access gk via indexes  
as in:  
\<code

# grid\_keys.rb

gk = [[1, 2], [0, 0], [3, 3] ]  
gk.each\_index { |i| puts “#{i} : #{gk[i].inspect}” }

# create a list of indexes in gk

pairs = []  
(0…gk.size-1).each do |index1|  
(index1+1…gk.size).each do |index2|  
pairs \<\< [index1, index2]  
end  
end  
puts “Pairs of indexes”  
pairs.each { |a| puts a.inspect }

puts “Indexes Grid\_Pairs”  
pairs.each do |idx|  
puts “#{idx.inspect} : #{gk[idx[0]].inspect} - #{gk[idx[1]].inspect}”  
end  
exit(0)  
**END**  
Result:  
ruby grid\_keys.rb  
0 : [1, 2]  
1 : [0, 0]  
2 : [3, 3]  
Pairs of indexes  
[0, 1]  
[0, 2]  
[1, 2]  
Indexes Grid\_Pairs  
[0, 1] : [1, 2] - [0, 0]  
[0, 2] : [1, 2] - [3, 3]  
[1, 2] : [0, 0] - [3, 3]  
``

HTH gfb
