From: Axel Etzold Date: 2007-09-29T23:58:06+09:00 Subject: Re: Is there a combination of a struct and an array? I wanna iterate over all created objects from a certain struct-class (I guess). -------- Original-Nachricht -------- > Datum: Sat, 29 Sep 2007 22:40:07 +0900 > Von: kazaam > An: ruby-talk@ruby-lang.org > Betreff: Is there a combination of a struct and an array? I wanna iterate over all created objects from a certain struct-class (I guess). > I have a file with many entries and much of these I don't need. Let's > imagine: > > name=arthur > age=16 > hobby=swimming > job=dancer > home=DE > name=marry > age=49 > hobby=singing > job=nurse > home=US > name=tom > age=32 > hobby=crafting > job=electician > home=NW > name=mantra > age=22 > hobby=reading > job=cop > home=FR > > this is my file and I need just the name,hobby and home of each one. > > Now I read my file: > > File.open(myfile).each { |line| > puts line.sub(/name=|hobby=|home=/,'') if line =~/name|hobby|home/ > } > > Which puts me the entries nicly on my screen. But I have to process them > from my script. So I need some kind of an array struct which let's me > iterate over each person and let's me use their name,hobby and home values. But > how to achieve this? With with variable-typ and how to get the correctly > collected? > > I wanna talk to them like this: > > person.each |entry| do > puts entry.name > puts entry.hobby > puts entry.home > end > > but how to achieve this? any idea or suggestion? > > bye > -- > kazaam Dear Kazaam, you could create a class Person to do the job, something like class Person def initialize(name,hobby,age,job,home) @name=name @hobby=hobby @age=age @job=job @home=home end def name @name end def hobby @hobby end def age @age end def home @home end def job @job end end me=Person.new("Albert Einstein","physics",134,"retired","wonttellyou") p me.hobby There is a nice introduction to classes in Ruby in http://pine.fm/LearnToProgram, particularly in chapter 9. Best regards, Axel -- GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS. Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/go/freemail