# Module Global Hash

**URL:** <https://rubytalk.org/t/module-global-hash/44074>\
**Category:** ruby-talk\
**Created:** [28 January 2008 05:06 UTC](https://rubytalk.org/t/module-global-hash/44074 "2008-01-28T05:06:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Lewis](https://avatars.discourse-cdn.com/v4/letter/r/c4cdca/32.png) [@Ryan\_Lewis](https://rubytalk.org/u/Ryan_Lewis)\
**Post date:** [28 January 2008 05:06 UTC](https://rubytalk.org/t/module-global-hash/44074/1 "2008-01-28T05:06:16Z")

</div>

#!/usr/bin/env ruby  
module Users  
&nbsp;&nbsp;class User \< Struct.new(:screenname, :password, :dob, :join\_date,  
:age)  
&nbsp;&nbsp;&nbsp;&nbsp;def initialize(sn, pw, dob)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.screenname = sn  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.password = pw  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.dob = Time.parse(dob)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.join\_date = Time.now  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.age = Time.now.year - Time.parse(dob).year  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

> **···**
>
> #########################################  
> &nbsp;&nbsp;user\_hash = {}
> 
> &nbsp;&nbsp;def new(sn, pw, dob)  
> &nbsp;&nbsp;&nbsp;&nbsp;Users::user\_hash[sn] = User.new(sn, pw, dob)  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;#########################################  
> &nbsp;&nbsp;module\_function :new  
> end
> 
> I'm trying to make it so when I call:  
> &nbsp;&nbsp;&nbsp;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 via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Wyatt\_Greene](https://avatars.discourse-cdn.com/v4/letter/w/82dd89/32.png) [@Wyatt\_Greene](https://rubytalk.org/u/Wyatt_Greene)\
**Post date:** [28 January 2008 05:20 UTC](https://rubytalk.org/t/module-global-hash/44074/2 "2008-01-28T05:20:02Z")

</div>

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.

> **···**
>
> On Jan 28, 12:06 am, Ryan Lewis \<c00lry...@gmail.com\> wrote:
> 
> > #!/usr/bin/env ruby  
> > module Users  
> > &nbsp;&nbsp;class User \< Struct.new(:screenname, :password, :dob, :join\_date,  
> > :age)  
> > &nbsp;&nbsp;&nbsp;&nbsp;def initialize(sn, pw, dob)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.screenname = sn  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.password = pw  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.dob = Time.parse(dob)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.join\_date = Time.now  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.age = Time.now.year - Time.parse(dob).year  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;#########################################  
> > &nbsp;&nbsp;user\_hash = {}
> > 
> > &nbsp;&nbsp;def new(sn, pw, dob)  
> > &nbsp;&nbsp;&nbsp;&nbsp;Users::user\_hash[sn] = User.new(sn, pw, dob)  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;#########################################  
> > &nbsp;&nbsp;module\_function :new  
> > end
> > 
> > I'm trying to make it so when I call:  
> > &nbsp;&nbsp;&nbsp;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/.

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [28 January 2008 05:57 UTC](https://rubytalk.org/t/module-global-hash/44074/3 "2008-01-28T05:57:05Z")

</div>

The way you've used it, user\_hash is a local variable for the module Users block. It disappears when the block ends.

You don't need a separate enclosing module. You've already got User.

In the following code, I used an instance variable of User, @user\_hash, to store the instances by name, User#initialize adds the entry, and for added ease of use, I defined User. and User.= as a way to access @user\_hash.

require 'time'  
class User \< Struct.new(:screenname, :password, :dob, :join\_date, :age)

&nbsp;&nbsp;&nbsp;class \<\<self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def (u)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user\_hash[u]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def =(name, struct)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user\_hash[name] = struct  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def user\_hash  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user\_hash ||= {}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def initialize(sn, pw, dob)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.screenname = sn  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.password = pw  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.dob = Time.parse(dob)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.join\_date = Time.now  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.age = Time.now.year - Time.parse(dob).year  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.class[sn] = self  
&nbsp;&nbsp;&nbsp;end  
end

User.new('bob', 'pw', '1/1/01')

p User['bob']  
p User.user\_hash

> **···**
>
> On Jan 28, 2008, at 12:06 AM, Ryan Lewis wrote:
> 
> > #!/usr/bin/env ruby  
> > module Users  
> > &nbsp;&nbsp;class User \< Struct.new(:screenname, :password, :dob, :join\_date,  
> > :age)  
> > &nbsp;&nbsp;&nbsp;&nbsp;def initialize(sn, pw, dob)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.screenname = sn  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.password = pw  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.dob = Time.parse(dob)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.join\_date = Time.now  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.age = Time.now.year - Time.parse(dob).year  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;#########################################  
> > &nbsp;&nbsp;user\_hash = {}
> > 
> > &nbsp;&nbsp;def new(sn, pw, dob)  
> > &nbsp;&nbsp;&nbsp;&nbsp;Users::user\_hash[sn] = User.new(sn, pw, dob)  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;#########################################  
> > &nbsp;&nbsp;module\_function :new  
> > end
> > 
> > I'm trying to make it so when I call:  
> > &nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Wyatt\_Greene](https://avatars.discourse-cdn.com/v4/letter/w/82dd89/32.png) [@Wyatt\_Greene](https://rubytalk.org/u/Wyatt_Greene)\
**Post date:** [28 January 2008 05:26 UTC](https://rubytalk.org/t/module-global-hash/44074/4 "2008-01-28T05:26:43Z")

</div>

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.

> **···**
>
> On Jan 28, 12:17 am, Wyatt Greene \<green...@yahoo.com\> wrote:
> 
> > On Jan 28, 12:06 am, Ryan Lewis \<c00lry...@gmail.com\> 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.

---

<div class="post-metadata">

**Author:** ![Ryan\_Lewis](https://avatars.discourse-cdn.com/v4/letter/r/c4cdca/32.png) [@Ryan\_Lewis](https://rubytalk.org/u/Ryan_Lewis)\
**Post date:** [28 January 2008 06:10 UTC](https://rubytalk.org/t/module-global-hash/44074/5 "2008-01-28T06:10:29Z")

</div>

Gary Wright wrote:

> &nbsp;&nbsp;&nbsp;class \<\<self

Hey, thanks man. I was thinking about defining and =, but I didnt  
know about \<\<self.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [28 January 2008 10:18 UTC](https://rubytalk.org/t/module-global-hash/44074/6 "2008-01-28T10:18:27Z")

</div>

> \> #!/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

> **···**
>
> 2008/1/28, Gary Wright \<gwtmp01@mac.com\>:
> 
> > On Jan 28, 2008, at 12:06 AM, Ryan Lewis wrote:
> 
> --  
> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Ryan\_Lewis](https://avatars.discourse-cdn.com/v4/letter/r/c4cdca/32.png) [@Ryan\_Lewis](https://rubytalk.org/u/Ryan_Lewis)\
**Post date:** [28 January 2008 05:35 UTC](https://rubytalk.org/t/module-global-hash/44074/7 "2008-01-28T05:35:48Z")

</div>

Wyatt Greene wrote:

> **···**
>
> > On Jan 28, 12:17 am, Wyatt Greene \<green...@yahoo.com\> wrote:
> > 
> > > \> self.password = pw  
> > > \> end
> > > 
> > > \> 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.
> 
> I thought of this, but I'm going to be using a system with many many  
> users and each time I would call Users, I would crate two classes,  
> taking up twice as much memory.
> 
> Should I just drop the module and just keep it in a Users.rb file to  
> call it later?  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [28 January 2008 13:22 UTC](https://rubytalk.org/t/module-global-hash/44074/8 "2008-01-28T13:22:05Z")

</div>

I like:

User = Struct.new(…) do

end

James Edward Gray II

> **···**
>
> On Jan 28, 2008, at 4:18 AM, Robert Klemme wrote:
> 
> > 2008/1/28, Gary Wright \<gwtmp01@mac.com\>:
> > 
> > > 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
> > 
> > >

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [28 January 2008 13:54 UTC](https://rubytalk.org/t/module-global-hash/44074/9 "2008-01-28T13:54:17Z")

</div>

Of course. Unfortunately it does not work with class constants  
properly. I should have mentioned that. (In my experiments I wanted  
to use User::USERS.)

Thanks for pointing it out!

Kind regards

robert

> **···**
>
> 2008/1/28, James Gray \<james@grayproductions.net\>:
> 
> > On Jan 28, 2008, at 4:18 AM, Robert Klemme wrote:
> > 
> > \> 2008/1/28, Gary Wright \<gwtmp01@mac.com\>:  
> > \>\>  
> > \>\> 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  
> > \>\>
> > 
> > I like:
> > 
> > User = Struct.new(…) do
> 
> --  
> use.inject do |as, often| as.you\_can - without end
