# Fill a table in ruby

**URL:** <https://www.ruby-forum.com/t/fill-a-table-in-ruby/186225>\
**Category:** Ruby\
**Created:** [April 2, 2010, 1:10pm UTC](https://www.ruby-forum.com/t/fill-a-table-in-ruby/186225 "2010-04-02T13:10:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brknl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ea924/32.png) [@brknl](https://www.ruby-forum.com/u/brknl)\
**Post date:** [April 2, 2010, 1:10pm UTC](https://www.ruby-forum.com/t/fill-a-table-in-ruby/186225/1 "2010-04-02T13:10:48Z")

</div>

Hi,

I want to read the datas from a file and fill a table with them.  
All lines will be a new row for table. And the columns are for the  
variables of the class.

I want to create an array as an object of a class for this. But i don’t  
know how to do it.

Class Abc  
@a  
@b  
…

end

a[100]=Abc.new()

so for example a[0] will be the first row of the table and a[0].a will  
be the first column of the table

i want to do sth like that.

How can i do it? Or is there another way to fill a table in ruby?

---

<div class="post-metadata">

**Author:** ![brknl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ea924/32.png) [@brknl](https://www.ruby-forum.com/u/brknl)\
**Post date:** [April 2, 2010, 1:33pm UTC](https://www.ruby-forum.com/t/fill-a-table-in-ruby/186225/2 "2010-04-02T13:33:53Z")

</div>

Sounds like a matrix class might work for you  
[http://ruby-doc.org/stdlib/libdoc/matrix/rdoc/classes/Matrix.html](http://ruby-doc.org/stdlib/libdoc/matrix/rdoc/classes/Matrix.html)

---

<div class="post-metadata">

**Author:** ![brknl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ea924/32.png) [@brknl](https://www.ruby-forum.com/u/brknl)\
**Post date:** [April 2, 2010, 4:40pm UTC](https://www.ruby-forum.com/t/fill-a-table-in-ruby/186225/3 "2010-04-02T16:40:35Z")

</div>

“Brk A.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> @a  
> i want to do sth like that.
> 
> ## How can i do it? Or is there another way to fill a table in ruby?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Use an array of structs as in the folloving snippet:

# 

class Table  
def initialize # define table structure and array for data  
@template = Struct.new(:name, :age)  
@data = []  
end # initialize

def add\_row(\_name, \_age)  
s=@template.new(\_name, \_age)  
@data \<\< s  
end # add\_row  
def out\_rows  
@data.each { |s| printf(“%-10s %4d\n”, s.name, s.age) }  
end # out\_rows

end # class Table

tbl = Table.new() # create a table and add some rows  
tbl.add\_row(‘frank’, 40)  
tbl.add\_row(‘tina’, 30)

tbl.out\_rows() # print the content of the table  
exit(0)  
**END**

### 

hth gfb

---

<div class="post-metadata">

**Author:** ![brknl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7ea924/32.png) [@brknl](https://www.ruby-forum.com/u/brknl)\
**Post date:** [April 5, 2010, 9:08am UTC](https://www.ruby-forum.com/t/fill-a-table-in-ruby/186225/4 "2010-04-05T09:08:38Z")

</div>

Thank you i was looking sth like that.
