0

I have an array called components

components = [
   {
      :name => "Component 1",
      :order => "1"
   },
   {
      :name => "Component 2",
      :order => "2"
   },
   {
      :name => "Component 3",
      :order => "3"
   },
   {
      :name => "Component 4",
      :order => "4"
   }
]

And another array called subcomponent

subcomponents = [
   {
      :name => "Subcomponent 1A",
      :order => "1A"
   },
   {
      :name => "Subomponent 1B",
      :order => "1B"
   },
   {
      :name => "Component 2A",
      :order => "2A"
    },
   {
      :name => "Component 4A",
      :order => "4A"
   }
]

I'm trying to get it so that the subcomponents appear underneath a component object if they are a subcomponent of that object. This is the expected output:

components = [
   {
      :name => "Component 1",
      :order => "1",
      :subcomponents => [
         {
            :name => "Subcomponent 1A",
            :order => "1A"
         },
         {
            :name => "Subomponent 1B",
            :order => "1B"
         }
      ]
   },
   {
      :name => "Component 2",
      :order => "2",
      :subcomponents => [ 
         {
            :name => "Component 2A",
            :order => "2A"
         }
      ]
   },
   {
      :name => "Component 3",
      :order => "3",
      :subcomponent => []
   },
   {
      :name => "Component 4",
      :order => "4",
      :subcomponent => [
         {
            :name => "Component 4A",
            :order => "4A"
         }
      ]
   }
]

I've created a loop to try to do this:

components.each do |c|
      c.class.module_eval { attr_accessor :subcomponents}
      c.subcomponents = []

      subcomponents.each do |s|
        if /#{c["order"]}[A-Z]/ =~ s["order"]
          #This is never assigned but it does make it into this statement 
          c.subcomponents << s
        end
      end 

     puts c.subcomponents.to_s # This prints []
end

As I've put in my code comments, the subcomponent is never assigned back to the component.subcomponents arrays even though the if statement is stepped into.

Am I missing something where the scope of component.subcomponents isn't accessible anymore or is at a different scope?

What should I be doing to ensure I can assign value component.subcomponents?

3 Answers 3

1
components.each do |c|
  c[:subcomponents] = []
  subcomponents.each do |s|
    c[:subcomponents] << s if /#{c[:order]}[A-Z]/ =~ s[:order]
  end 
end
Sign up to request clarification or add additional context in comments.

Comments

0

Hope this work.

components.each do |component|
  component[:subcomponents] = []
end

subcomponents.each do |subcomponent|
  order = subcomponent[:order][/\d+/]

  components.each do |component|
    if order == component[:order]
      component[:subcomponents] << subcomponent
    end
  end
end

Comments

0

You can do it in O(n) using this:

subcomponent_hash = Hash.new { |h, k| h[k] = [] }
subcomponents.each do |sc|
  order = sc[:order][/\d+/]
  subcomponent_hash[order] << sc
end

components.each do |c|
  c[:subcomponents] = subcomponent_hash[c[:order]]
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.