From: Robert Klemme Date: 2008-01-28T19:18:27+09:00 Subject: Re: Module Global Hash 2008/1/28, Gary Wright : > > On Jan 28, 2008, at 12:06 AM, Ryan Lewis wrote: > > > #!/usr/bin/env ruby > > module Users > > class User < Struct.new(:screenname, :password, :dob, :join_date, > > :age) You do not need to inherit from the new struct class, instead you can do User = Struct.new(:screenname, :password, :dob, :join_date, :age) class User ... end > > def initialize(sn, pw, dob) > > self.screenname = sn > > self.password = pw > > self.dob = Time.parse(dob) > > self.join_date = Time.now > > self.age = Time.now.year - Time.parse(dob).year > > end > > end > > ######################################### > > user_hash = {} > > > > def new(sn, pw, dob) > > Users::user_hash[sn] = User.new(sn, pw, dob) > > end > > ######################################### > > module_function :new > > end > > > > I'm trying to make it so when I call: > > Users::new("screenname", "pass", "1/15/91") > > > > it adds a new instance of the User class into user_hash, but when I > > execute the above code in IRB I get a NoMethodError for user_hash for > > the Users:module Note though that this approach will prevent any User instance from being garbage collected. This might or might not be what you want. I do not know the use case but there is another option: you can traverse all User instances via ObjectSpace. Ah, and another remark: the implementations show so far will also allow to create two users with the same name but only one of them will be accessible via the Hash. Kind regards robert -- use.inject do |as, often| as.you_can - without end