From: Alan Chen Date: 2002-02-24T06:51:18+09:00 Subject: Re: Newbie question On Sun, Feb 24, 2002 at 06:05:25AM +0900, roktas wrote: > Hi all, > > How can I create an array of array with some funny (!) properties? To > make clear, here is the exact problem: I have a tab seperated text file > which the first line points to the field labels, that is; > > field1\tfield2\tfield3 > foo\tbar\tgoo > .. > > I have created a 'DB' class which holds the whole data (after some > parsing). What I want to realize is something that results with the > following simple calls in the main block: > > db = DB.new("file.txt") > p db[3]["field2"] Maybe it would help for me to break down this syntax more: row3 = db[3] # row3 I presume is now your Record Class, this calls DB::[] for the db instance p row3["field2"] # this would call Record::[] for the row3 instance The definition for the access function for Record might go like this class Record def []( field_name ) # lookup field_name in DB fields return field_value end end it might be easier for class Record to be inherited from Hash. The behavior of Hash is closer to how you're using Record. For this simple usage, you could use Hash directly, but I assume you might want to add more customized behavior to Record. > I have some experimental codes but don't include them to prevent messing > around. Inside a 'Record' class (inherited from Array), I have played > with [] method to take a string argument (like "field2" as shown above) > and used this class as the line container in a base array, but can't > succeed :( > > This is my first attemp to learn Ruby's OOP features, but perhaps, as a > first try, I could choose another problem :) Thanks in advance for your > replies and sorry for my poor English. Regards. > > -- > roktas > -- Alan Chen Digikata LLC http://digikata.com