From: Wyatt Greene Date: 2008-01-28T14:26:43+09:00 Subject: Re: Module Global Hash On Jan 28, 12:17 am, Wyatt Greene wrote: > On Jan 28, 12:06 am, Ryan Lewis wrote: > > > > > #!/usr/bin/env ruby > > module Users > > class User < Struct.new(:screenname, :password, :dob, :join_date, > > :age) > > 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 > > > I've also tried putting both a @ and @@ infront of user_hash but irb > > comes up with the same error. > > -- > > Posted viahttp://www.ruby-forum.com/. > > Remember that in Ruby, everything is an object and objects communicate > by passing messages (calling methods). So you can't get to variables > of an object (such as user_hash). You can only call methods that an > object responds to. I think what's going on here is that when the parser gets to the line that says user_hash = {} it looks for a method called user_hash to execute. It doesn't realize that you're trying to instantiate a variable. Maybe you should consider turning the Users module into a class and trying @@user_hash = {} again.