Prev: require rubygems
Next: A little help needed
From: Brk Anl on 2 Apr 2010 07:10 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/.
From: jbw on 2 Apr 2010 07:33 [Note: parts of this message were removed to make it a legal post.] Sounds like a matrix class might work for you http://ruby-doc.org/stdlib/libdoc/matrix/rdoc/classes/Matrix.html On Fri, Apr 2, 2010 at 12:10 PM, Brk Anl <banil86(a)yahoo.com> wrote: > 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/. > > -- jbw
From: GianFranco Bozzetti on 2 Apr 2010 10:39 "Brk Anl" <banil86(a)yahoo.com> wrote in message news:104aef4810489fd18812714cd885f263(a)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=(a)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
From: Brk Anl on 5 Apr 2010 03:08 Thank you i was looking sth like that. -- Posted via http://www.ruby-forum.com/.
|
Pages: 1 Prev: require rubygems Next: A little help needed |