开发者

What's the best way to organize 32 items into groups in ruby? [closed]

开发者 https://www.devze.com 2023-04-02 03:29 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post.
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 9 years ago.

Improve this question

Which 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] 
0

精彩评论

暂无评论...
验证码 换一张
取 消