# Hashed matrix

**URL:** <https://rubytalk.org/t/hashed-matrix/52952>\
**Category:** ruby-talk\
**Created:** [20 April 2009 10:04 UTC](https://rubytalk.org/t/hashed-matrix/52952 "2009-04-20T10:04:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yoann\_Moreau](https://avatars.discourse-cdn.com/v4/letter/y/3d9bf3/32.png) [@Yoann\_Moreau](https://rubytalk.org/u/Yoann_Moreau)\
**Post date:** [20 April 2009 10:04 UTC](https://rubytalk.org/t/hashed-matrix/52952/1 "2009-04-20T10:04:37Z")

</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 ?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [20 April 2009 11:01 UTC](https://rubytalk.org/t/hashed-matrix/52952/2 "2009-04-20T11:01:57Z")

</div>

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  
&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@hash = Hash.new {|h,k| h[k] = State.new(0, "")}  
&nbsp;&nbsp;end

&nbsp;&nbsp;def (row,col)  
&nbsp;&nbsp;&nbsp;&nbsp;@hash[[row, col]]  
&nbsp;&nbsp;end

&nbsp;&nbsp;def =(row,col, val)  
&nbsp;&nbsp;&nbsp;&nbsp;raise ArgumentError, "Not a state %p" % val  
&nbsp;&nbsp;&nbsp;&nbsp;@hash[[row, col]] = val  
&nbsp;&nbsp;end  
end

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

Kind regards

robert

> **···**
>
> 2009/4/20 Yoann Moreau \<yoni612@hotmail.com\>:
> 
> > 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,  
> > "")) }
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [20 April 2009 11:02 UTC](https://rubytalk.org/t/hashed-matrix/52952/3 "2009-04-20T11:02: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,  
> > "")) }
> 
> 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 (row,col)  
> @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

> **···**
>
> 2009/4/20 Robert Klemme \<shortcutter@googlemail.com\>:
> 
> > 2009/4/20 Yoann Moreau \<yoni612@hotmail.com\>:
> 
> > @hash[[row, col]] = val  
> > end  
> > end
> > 
> > This is likely more efficient if your matrices are large and sparse.
> > 
> > Kind regards
> > 
> > robert
> > 
> > --  
> > remember.guy do |as, often| as.you\_can - without end  
> > [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Yoann\_Moreau](https://avatars.discourse-cdn.com/v4/letter/y/3d9bf3/32.png) [@Yoann\_Moreau](https://rubytalk.org/u/Yoann_Moreau)\
**Post date:** [20 April 2009 11:15 UTC](https://rubytalk.org/t/hashed-matrix/52952/4 "2009-04-20T11:15:29Z")

</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.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
