From: GianFranco Bozzetti Date: 2010-04-02T23:40:13+09:00 Subject: Re: Fill a table in ruby "Brk Anl" wrote in message news:104aef4810489fd18812714cd885f263@ruby-forum.com... > 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? > -- > Posted via 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