Dvex
Dvex

Reputation: 971

How to put array into array in Ruby

I'm doing a family tree. So, I'm forming the order from the oldest to the newest family member.

For this, I'm forming an array that has the structure of the final tree and then I'm only print the result.

In PHP I can do something like this:

Array(
'member1' => [
  'sub_member11' => [
    ['sub_sub_member111' => some_value_of_sons],
    ['sub_sub_member112' => some_value_of_sons]
  ],
  ['sub_member12' => some_value_of_sons]
],
'member2' => [
  ['sub_member21' => some_value_of_sons],
  ['sub_member22' => some_value_of_sons]
]
)

In Ruby, I would like to do something like this. Sorry for my english.

Upvotes: 0

Views: 110

Answers (2)

Mark Thomas
Mark Thomas

Reputation: 37527

I would suggest using RubyTree for this, rather than a data structure. There are several advantages:

  • Some of the queries you may need to do have already been implemented efficiently
  • The naming conventions match your domain (parent/grandparent/siblings/children etc)
  • It will shorten your code considerably and make it easier to read
  • There are built-in ways to traverse and print the tree

Here's an excerpt from the documentation, which will help you get a feel for it:

require 'tree'     

# ..... Create the root node first.  Note that every node has a name and an optional content payload.
root_node = Tree::TreeNode.new("ROOT", "Root Content")
root_node.print_tree

# ..... Now insert the child nodes.  Note that you can "chain" the child insertions for a given path to any depth.
root_node << Tree::TreeNode.new("CHILD1", "Child1 Content") << Tree::TreeNode.new("GRANDCHILD1", "GrandChild1 Content")
root_node << Tree::TreeNode.new("CHILD2", "Child2 Content")

# ..... Lets print the representation to stdout.  This is primarily used for debugging purposes.
root_node.print_tree

# ..... Lets directly access children and grandchildren of the root.  The can be "chained" for a given path to any depth.
child1       = root_node["CHILD1"]
grand_child1 = root_node["CHILD1"]["GRANDCHILD1"]

# ..... Now lets retrieve siblings of the current node as an array.
siblings_of_child1 = child1.siblings

# ..... Lets retrieve immediate children of the root node as an array.
children_of_root = root_node.children

Note that the examples show the node content as a string, but you can put any object there. You may want to create a value object to hold all your metadata.

class FamilyMember
  attr_accessor :name :last_name, :maiden_name, :birth_date, :etc
end

Hopefully after doing all of this your code will be more like uncle_bob.children.first.birth_date -- very readable.

Upvotes: 0

David Grayson
David Grayson

Reputation: 87541

I think you are looking for Ruby's Hash data type. You can create a new hash using syntax like this:

{ key1 => value1, key2 => value2 }

So you could make a hash with your desired data in it by writing:

hash = {
  'member1' => {
    'sub_member11' => {
      'sub_sub_member111' => some_value_of_sons,
      'sub_sub_member112' => some_value_of_sons,
    },
    'sub_member12' => some_value_of_sons,
  },
  'member2' => {
     'sub_member21' => some_value_of_sons,
     'sub_member22' => some_value_of_sons,
  },
}

Hashes are used very commonly in Ruby programs, so it will pay off to understand them and read the documentation:

http://ruby-doc.org/core/Hash.html

Upvotes: 3

Related Questions