Sebastian Hungerecker
10/2/2007 6:51:00 PM
Lee Jarvis wrote:
> 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
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
--
NP: Black Sabbath - Hand of Doom
Jabber: sepp2k@jabber.org
ICQ: 205544826