# Named Matrix like ArrayFields

**URL:** <https://www.ruby-forum.com/t/named-matrix-like-arrayfields/173896>\
**Category:** Ruby\
**Created:** [August 17, 2009, 11:16pm UTC](https://www.ruby-forum.com/t/named-matrix-like-arrayfields/173896 "2009-08-17T23:16:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dbf](https://www.ruby-forum.com/u/dbf)\
**Post date:** [August 17, 2009, 11:16pm UTC](https://www.ruby-forum.com/t/named-matrix-like-arrayfields/173896/1 "2009-08-17T23:16:05Z")

</div>

I’m looking for a named matrix that acts like arrayfields so you can  
get and set cells based on a name and/or an index. This is array  
fields \<[http://codeforpeople.com/lib/ruby/arrayfields/](http://codeforpeople.com/lib/ruby/arrayfields/)  
arrayfields-4.7.0/\> :

> > require ‘arrayfields’  
> > =\> true  
> > x = ArrayFields.new(:a,:b,:c,:d)  
> > =\> [:b, :d]  
> > x[0]  
> > =\> :b  
> > x[:a]  
> > =\> :b  
> > x[‘a’]  
> > =\> :b

I basically want that but in two dimensions.

Something like:

x = NamedMatrix.new  
x.xs = %w(a b c d) # the names of the x axis  
x.ys = %w(e f g h) # the names of the y axis

So that x[:a][:f] is the same as x[0][1] for getting and setting. Also  
have some dumping mechanism to get an array of arrays or arrays of  
hashes or even csv or some other 2D dump.

I don’t think this is too tough to write especially with arrayfields  
but was wondering if anyone has done it yet.

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![dbf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dbf](https://www.ruby-forum.com/u/dbf)\
**Post date:** [August 19, 2009, 2:52pm UTC](https://www.ruby-forum.com/t/named-matrix-like-arrayfields/173896/2 "2009-08-19T14:52:47Z")

</div>

> 

Maybe take a look at Struct.

x = Struct.new(:a,:b,:c)  
y = Struct.new(:d,:e,:f)  
var = x.new(y.new(0,1,2),y.new(10,11,12),y.new(20,21,22))

#################  
p var[:c][:e]  
p var[2][1]  
p var[‘c’][“e”]

var[2][:e] = 77  
p var[:c][:e]

Harry
