Basically I'm working with a 2D matrix. I can access elements of the matrix by specifying an (x,y) pair to get the corresponding value at that position.
Now I also want to be able to keep track of certain pairs that are arbitrarily determined at run-time. For example, maybe I need to keep track of the values at (1,2), (3,4), and (5,6), and maybe I need to retrieve the value at that position frequently.
So I was thinking how about just make a hash .
liked_elements = {[1,2] => M[1,2], [3,4] =>M[3,4], [5,6]=>M[5,6]}
Or something like that.
Then I can quickly iterate over the hash and get the elements that I like.
Are there any issues with using arrays as hash keys?
If it's truly a matrix (an array of arrays), then you can just pass in coordinates like this
Yes, you can create an array as a hash key.
Just don't modify the array afterward (or remember to rehash the hash if you do).
I am currently making a two-dimensional array where each array points to a specific hash-map. Each map hold their own specific data and I would like to retrieve key, and value information. However, when I reference an array (i.e. A[0][1] I cannot access the hash functions.
I checked the class type by using A.class to verify it was a Hash and indeed that is what it returns. Is this an inherent problem with Ruby 1.9.3 or am I doing something wrong?