From: Kent Dahl Date: 2002-03-14T20:26:12+09:00 Subject: Re: Newbie -- Please help!! "Firestone, Mark - Technical Support" wrote: > Second, some backround... I am attempting to learn object orientated > programming. I used to write programs in things you guys will probably not > like, such as Turbo Pascal. Turbo Pascal rocked... Back in the stone age, that is :-) I had a little gander, and did some modifications. No global variable in sight. :-) ----------------8<------------------------ class User def initialize(name,phone,citystate,address,password) @deleted = FALSE @locked = TRUE @name = name @alais = '' @alaisOn = FALSE @phone = phone @citystate = citystate @address = address @password = password @width = 80 end ## def name ## @name ## end ## ## def password ## @password ## end attr_reader :name, :password # short and neat def show print "User: #@name #@phone #@citystate #@address #@password\n" end end class User_list include Enumerable def initialize @users = Array.new end def append(aUser) @users.push(aUser) self end def [](key) if key.kind_of?(Integer) result = @users[key] else result = @users.find { |user| key == user.name } end return result end ## end #of Class User_list ## class method of User_list, so we don't need instance. def self.loadusers list = nil File.open("users.dat") do |f| list = Marshal.load(f) ## print "- Loading User Object...\n" end return list ## end # save list def saveusers File.open("users.dat", "w+") do |f| Marshal.dump( self, f) ## end print "- Saving User Object...\n" end def checkpassword (username,password) result = FALSE if self[username] != nil ## if self[username].password == password ## result = TRUE end else print "You passed me a bad user -- ass-munch!\n" end return result end ## new end... end #of Class User_list ## if File.exists?("users.dat") list = User_list.loadusers else list = User_list.new list.append(User.new('SYSOP','000-000-0000','Tempe, AZ','600 E. Solana Drive','STUPID')) list.append(User.new('TEST','000-000-0000','Mesa, AZ','123 Sample Street','HAPPY')) end list[0].show list[1].show if list['TEST'] != nil list['TEST'].show end print list.checkpassword('SYSOP','STUPID') print list.checkpassword('SYSOP2','PASSWORD') list.saveusers ----------------8<------------------------ More of the functionality has been encapsulated in your User_list class. Should be a tad more cleaner. HTH. I'd comment more, but I have to catch a bus... -- <[ Kent Dahl ]>================<[ http://www.stud.ntnu.no/~kentda/ ]> )__(stud.techn.; ind. econ & management: computer technology)__( /"Opinions expressed are mine and not those of my Employer, "\ ( "the University, my girlfriend, stray cats, banana fruitflies, " ) \"nor the frontal lobe of my left cerebral hemisphere. "/