Ruby help. How should I recurse through this data to be display it?

77 views Asked by At

So I have data that I am trying to learn from that looks similar to what I posted lower down. For simplicity of explaining my needs I will say that I am trying to pull the id from each item in the array and then each id of the values of that object in the array. and so on and so on. I have tried a lot of ways of doing this but don't feel like I have a firm grasp on what the solution would actually look like. I am not yet great with recursion but this seems to be a situation where this lack of knowledge is catching up. Please let me know if you need more information but my main goal is to figure out how to access this data in order of the parent for each.

    objects = [
  {
    id: 100,
    values: [
      {
        id: 101,
        values: [
          {
            id: 103,
            values: [],
            last: true,
            parent: 101
          }
        ],
        last: false,
        parent: 100
      },
      obj_c: {
        id: 102,
        values: [
          {
            id: 104,
            values: [
              {
                id: 105,
                values: [],
                last: true,
                parent: 104
              }
            ],
            last: false,
            parent: 102
          }
        ],
        last: false,
        parent: 100
      }
    ],
    last: false,
    parent: nil
  },
  {
    id: 106,
    values: [
      {
        id: 107,
        values: [
          {
            id: 110,
            values: [],
            last: true,
            parent: 107
          }
        ],
        last: false,
        parent: 106
      },
      {
        id: 108,
        values: [
          {
            id: 109,
            values: [],
            last: true,
            parent: 108
          }
        ],
        last: false,
        parent: 106
      }
    ],
    last: false,
    parent: nil
  }
]

So with that sample data I am wondering how I would get it to look like this:

100
    101
        103
    102
        104
            105


106
    107
        110
    108
        109

So even if I were to just figure out how to print it to the console in this manner with the proper amount of whitespace to indicate which parent id each piece is coming from would solve my problem. But hopefully this helps indicate what I am trying to acheive. Thanks in advance for any advice on how to approach this.

1

There are 1 answers

1
Tarek N. Elsamni On BEST ANSWER

This is a recursion problem. You can solve it either using recursion function/method calls (implicit usage of stacks) or explicit stack data structure.

Below is how to solve it using recursion calls:

def print_object(object, depth)
  print " " * depth
  puts object[:id]
  object[:values].each do |o|
    print_object(o, depth + 1)
  end if object[:values]
end

objects.each do |object|
  print_object(object, 0)
end

When programming a recursion solution always think about: 1- Initial state. 2- Termination state. 3- Recursive pattern.

So in this example, I've decided that I'll be recursively printing out the results with extra space for deeper level (depth + 1) # recursive pattern, and I'll start with 0 # initial state spaces at the beginning and I'll stop/return when there no more object[:values] # termination state.