Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Hash of hashes and arrays?

Well, I was wondering if is possible... (I mean yes, because I've seen before around there) to make a hash with differents objects belong to 1 master object, and return these values and print them.

Something like this:


Code:
module Something

Meph = {'name' => ['Icon', 'Variable', 'Description']}

end

And after, show these results:

Think about a selectable scene, when each command is a index of the Something::Meph

Code:
index = @command_index[Something::Meph[index]]

self.contents.draw_text(4, 32, 120, 32, name[@index], 2)
self.contents.draw_text(4, 100, 120, 32, icon[@index], 2)
self.contents.draw_text(4, 32, 120, 32, variable[@index], 2)

That was only an 'IDEA' of what that I want..

Well, that is my question.. Thanks for asnwer it.
 

Atoa

Member

@mephisto
Yes it is.
I do this very frequently in my scripts.

But not the way you did.
since the Hash index is an string, you would need make the code an bit different than you did.
You can't compare an string to an integer.

If maybe you do something more this like:
Meph = { index => ['Icon', 'Variable', 'Description']}

self.contents.draw_text(4, 32, 120, 32,Something::Meph[@index][0] , 2)
self.contents.draw_text(4, 100, 120, 32,Something::Meph[@index][1], 2)
self.contents.draw_text(4, 32, 120, 32, Something::Meph[@index][2], 2)

or maybe:
Code:
Meph = { 'name' => ['Icon', 'Variable', 'Description']}

comand = Something::Meph[ @commands[index]]

self.contents.draw_text(4, 32, 120, 32,comand[0] , 2)
self.contents.draw_text(4, 100, 120, 32,comand[1], 2)
self.contents.draw_text(4, 32, 120, 32, comand[2], 2)
 
Ok. Arrays and Hashes are list with keys and values. Arrays are ordered with the indexes being values from 0 to the size of the array - 1. Hashes are unordered with indexes being any specified object.

Code:
array = [2, 6, 2, 1]
# is basically the same as
hash = {0 => 2, 1 => 6, 2 => 2, 3 => 1}

So with your hash above:
Code:
Meph['something']   # -> nil
Meph['name']   # -> ['Icon', 'Variable', 'Description']
Meph['name'][0]  # -> 'Icon'
Meph['name'][1]  # -> 'Variable'

Just remember, you are dealing with objects.




.each depends on your object.

If you are dealing with Arrays

Code:
array = [1, 2, 3, 7, 8, 9]
array.each do |i|
  p i
end
# -> 123789

# same as
array = [1, 2, 3, 7, 8, 9]
array.each {|i| p i }

do...end is the same as {...}

If you are dealing with hashes
Code:
hash = {1=>7, 2=>8, 3=>9}
hash.each do |key, value|
  p key, value
end
# -> 172839

Hope that helps a little. Is there anything you are trying to do with this?
 

Zeriab

Sponsor

I would say considering hashes as a mapping from keys to objects rather than an unordered list is preferable.
Consider like you have a bunch of lockers. In each locker there can be one or no objects. Each key leads to exactly one locker. From that one key you go the locker and find the object in there. Note that more keys can be assigned to the same locker, but not more than one locker. (No master keys in hashes)

Note that you can consider rethinking your design.
Usually having arrays of arrays of arrays and so on or hashes of hashes of hashes and so on or a mix is an indication of bad design.
Such structures typically creates very tight coupling and the objects dealing with them must know very much about their intrinsic structure.
Chances are that you can choose a more loosely coupled solution where you encapsulate the functionality you want in a couple of classes introduced for the purpose which is much more manageable and with negligible overhead.

*hugs*
- Zeriab
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top