# Hashes

**URL:** <https://www.ruby-forum.com/t/hashes/116140>\
**Category:** Ruby\
**Created:** [October 2, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/hashes/116140 "2007-10-02T20:29:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jarvo88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a8b319/32.png) [@jarvo88](https://www.ruby-forum.com/u/jarvo88)\
**Post date:** [October 2, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/hashes/116140/1 "2007-10-02T20:29:58Z")

</div>

I want to create a large hash kind of information field… basically i  
have a room (with a specific name, topic, nicknames and id) it might  
look like this

name = room1  
topic = room1 topic  
nicks = [nick1, nick2, nick3 …]  
id = 101

but i have more then 1 room, and would like something like this

rooms =  
room1 =  
topic = room1 topic  
nicks = [nick1, nick2, nick3]  
id = 101  
room2 =  
topic = room2 topic  
nicks = [nick1, nick2, nick3 …]  
id = 102

But i wouldn’t know how to do it, i need to be able to access all of the  
values easily according to the room, is there an easy way to do this?

also, why do i see a lot of hashes like this format: hash = { :one =\>  
“something” } (im not sure i get what the :one part does…)

Thanks in advance

---

<div class="post-metadata">

**Author:** ![jarvo88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a8b319/32.png) [@jarvo88](https://www.ruby-forum.com/u/jarvo88)\
**Post date:** [October 2, 2007, 8:51pm UTC](https://www.ruby-forum.com/t/hashes/116140/2 "2007-10-02T20:51:27Z")

</div>

Lee J. wrote:

> ```
> id = 102
> 
> ```

Hash of structs:  
Room=Struct.new(:topic,:nicks,:id)  
rooms={“room1” =\> Room.new(“room1 topic”, [nick1, nick2, nick3], 101),  
“room2” =\> Room.new(“room2 topic”, [nick1, nick2, nick2], 102) }

rooms[“room1”].topic #=\> “room1 topic”  
rooms[“room2”].id #=\> 102

Hash of hashes:  
rooms={“room1” =\> { :topic =\> “room1 topic”,  
:nicks =\> [nick1,nick2],  
:id =\> 101 },  
“room2” =\> { :topic =\> “room2 topic”,  
:nicks =\> [nick1,nick2],  
:id =\> 102 } }

rooms[“room1”][:topic] #=\> “room1 topic”  
rooms[“room2”][:id] #=\> 102

I prefer the first version.

> also, why do i see a lot of hashes like this format: hash = { :one =\>  
> “something” } (im not sure i get what the :one part does…)

:one is the key and “something” is the value. So hash[:one] would return  
“something”. If you’re confused by :one being a symbol (instead of e.g.  
a  
string), be aware that that is in no way mandatory. As you see in the  
above  
code, the keys can as well be strings (or anything else). Usually you  
use  
strings when the keys are arbitrary (like roomnames) and symbols  
otherwise  
(like in the nested hashs where the keys are always :topic,:nicks and  
:id).

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![jarvo88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a8b319/32.png) [@jarvo88](https://www.ruby-forum.com/u/jarvo88)\
**Post date:** [October 2, 2007, 9:21pm UTC](https://www.ruby-forum.com/t/hashes/116140/3 "2007-10-02T21:21:26Z")

</div>

Thanks a lot, that was perfect 🙂

One query though, i used structs and had this code

Room=Struct.new(:topic,:nicks,:id)  
rooms={“room1” =\> Room.new(“room1 topic”, [nick1, nick2, nick3], 101),  
“room2” =\> Room.new(“room2 topic”, [nick1, nick2, nick2], 102) }

lets say the topic of room 1 changes, well

rooms[“room1”].topic = “new topic”

won’t work, is there a reason? or another way to change it?

Thanks again for the help

---

<div class="post-metadata">

**Author:** ![jarvo88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a8b319/32.png) [@jarvo88](https://www.ruby-forum.com/u/jarvo88)\
**Post date:** [October 2, 2007, 9:32pm UTC](https://www.ruby-forum.com/t/hashes/116140/4 "2007-10-02T21:32:30Z")

</div>

Lee J. wrote:

> Room=Struct.new(:topic,:nicks,:id)  
> rooms={“room1” =\> Room.new(“room1 topic”, [nick1, nick2, nick3], 101),  
> “room2” =\> Room.new(“room2 topic”, [nick1, nick2, nick2], 102) }
> 
> lets say the topic of room 1 changes, well
> 
> rooms[“room1”].topic = “new topic”
> 
> won’t work, is there a reason?

Works here:

> > Room=Struct.new(:topic,:nicks,:id)  
> > =\> Room
> 
> > rooms={“room1” =\> Room.new(“room1 topic”, [],101),  
> > ?\> “room2” =\> Room.new(“room2 topic”, [], 102) }  
> > =\> {“room1”=\>#\<struct Room topic=“room1 topic”, nicks=[],  
> > id=101\>, “room2”=\>#\<struct Room topic=“room2 topic”, nicks=[], id=102\>}
> 
> > rooms[“room1”].topic  
> > =\> “room1 topic”
> 
> > rooms[“room1”].topic = “bla”  
> > =\> “bla”
> 
> > rooms[“room1”].topic  
> > =\> “bla”

---

<div class="post-metadata">

**Author:** ![jarvo88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a8b319/32.png) [@jarvo88](https://www.ruby-forum.com/u/jarvo88)\
**Post date:** [October 2, 2007, 9:54pm UTC](https://www.ruby-forum.com/t/hashes/116140/5 "2007-10-02T21:54:45Z")

</div>

> Works here:
> 
> > > Room=Struct.new(:topic,:nicks,:id)  
> > > =\> Room
> > 
> > > rooms={“room1” =\> Room.new(“room1 topic”, [],101),  
> > > ?\> “room2” =\> Room.new(“room2 topic”, [], 102) }  
> > > =\> {“room1”=\>#\<struct Room topic=“room1 topic”, nicks=[],  
> > > id=101\>, “room2”=\>#\<struct Room topic=“room2 topic”, nicks=[], id=102\>}
> > 
> > > rooms[“room1”].topic  
> > > =\> “room1 topic”
> > 
> > > rooms[“room1”].topic = “bla”  
> > > =\> “bla”
> > 
> > > rooms[“room1”].topic  
> > > =\> “bla”

Ahh yeah, i was being silly and working with 2 files, and running the  
un-modified one, its been a long day

That’s a lot for your help
