# Hashed matrix

**URL:** <https://www.ruby-forum.com/t/hashed-matrix/165960>\
**Category:** Ruby\
**Created:** [April 20, 2009, 12:04pm UTC](https://www.ruby-forum.com/t/hashed-matrix/165960 "2009-04-20T12:04:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yoniblond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9d8465/32.png) [@yoniblond](https://www.ruby-forum.com/u/yoniblond)\
**Post date:** [April 20, 2009, 12:04pm UTC](https://www.ruby-forum.com/t/hashed-matrix/165960/1 "2009-04-20T12:04:32Z")

</div>

Hello, I’d like to use a specific matrix with string as key for lines  
and integer index for columns. I could also use string for columns (a  
string version of integer), it doesn’t matter. I use the matrix to store  
a small object (named State) I just need to access data like that :  
matrix[a\_string][a\_number]

Here is how I tried to do it :  
matrix = Hash.new{ |hash, key| hash[key] = Array.new(5, State.new(0,  
“”)) }

But I have strange values, is there a better way to make that matrix ?

---

<div class="post-metadata">

**Author:** ![yoniblond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9d8465/32.png) [@yoniblond](https://www.ruby-forum.com/u/yoniblond)\
**Post date:** [April 20, 2009, 1:02pm UTC](https://www.ruby-forum.com/t/hashed-matrix/165960/2 "2009-04-20T13:02:44Z")

</div>

2009/4/20 Yoann M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hello, I’d like to use a specific matrix with string as key for lines  
> and integer index for columns. I could also use string for columns (a  
> string version of integer), it doesn’t matter. I use the matrix to store  
> a small object (named State) I just need to access data like that :  
> matrix[a\_string][a\_number]
> 
> Here is how I tried to do it :  
> matrix = Hash.new{ |hash, key| hash[key] = Array.new(5, State.new(0,  
> “”)) }

You want the block form of Array.new because otherwise all array  
entries point to the same object:

matrix = Hash.new{ |hash, key| hash[key] = Array.new(5) {  
State.new(0,“”) } }

Alternative approach: use an Array of String and Fixnum as Hash key:

class Matrix  
def initialize  
@hash = Hash.new {|h,k| h[k] = State.new(0, “”)}  
end

def   
@hash[[row, col]]  
end

def []=(row,col, val)  
raise ArgumentError, “Not a state %p” % val  
@hash[[row, col]] = val  
end  
end

This is likely more efficient if your matrices are large and sparse.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![yoniblond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9d8465/32.png) [@yoniblond](https://www.ruby-forum.com/u/yoniblond)\
**Post date:** [April 20, 2009, 1:03pm UTC](https://www.ruby-forum.com/t/hashed-matrix/165960/3 "2009-04-20T13:03:19Z")

</div>

2009/4/20 Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> end
> 
> def   
> @hash[[row, col]]  
> end
> 
> def []=(row,col, val)  
> raise ArgumentError, “Not a state %p” % val

This should have read

raise ArgumentError, “Not a state %p” % val unless State === val

---

<div class="post-metadata">

**Author:** ![yoniblond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9d8465/32.png) [@yoniblond](https://www.ruby-forum.com/u/yoniblond)\
**Post date:** [April 20, 2009, 1:15pm UTC](https://www.ruby-forum.com/t/hashed-matrix/165960/4 "2009-04-20T13:15:22Z")

</div>

Thanks for your answer, you’re right I had the same object in my array,  
this is why I had strange values.  
I use a really small matrix so it’s not a big deal to be efficient, I’ll  
use your first idea.

Thank you.
