Hashes

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

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

Thanks a lot, that was perfect :slight_smile:

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

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”

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs