What's the best way to organize 32 items into groups in ruby? [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this questionWhich data structure is best to organize 32 items. I have 8 groups of 4 items.
Right now I have each item defined and put into an array of 4 (did that 8 times).
Then I took 4 of those arrays of 4 and put them into another array (so the group is half of the total size, i.e. 16).
Then I made a an array with two items, wh开发者_如何学运维ere each entry in the array had 16 items in it.
I'm new to Ruby but there has to be a better way to structure this data.
Help is much appreciated.
Use a hash http://www.ruby-doc.org/core/classes/Hash.html you could also benefit from just writing your own classes I suppose.
If you just use the native classes it's very easy to fit the data structures to what you need:
ree-1.8.7-2010.02 :036 > hsh = {"big"=>[234234, 234243, 23242], "small"=>[1, 2, 3]}
=> {"big"=>[234234, 234243, 23242], "small"=>[1, 2, 3]}
# access all the values in a single array
ree-1.8.7-2010.02 :037 > hsh.values.flatten
=> [234234, 234243, 23242, 1, 2, 3]
精彩评论