Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8638573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:45:55+00:00 2026-06-12T10:45:55+00:00

nodes = { :node1 => { :inherits => nil, :variables => { :foo =>

  • 0
nodes = {
  :node1 => { 
    :inherits => nil, 
    :variables => { :foo => 1, :bar => 2 } 
  },
  :node2 => { 
    :inherits => :node1, 
    :variables => { :foo => 9, :baz => 4 } 
  }
}

I’ve been trying to understand how to return a new nodes hash where each node’s :variables hash is merged with :variables from the node specified in :inheritance. In other words, node1 would be left as is while node2 to end up with :variabes => { :foo => 9, :bar => 2, :baz => 4 }

I’ve been swimming through docs involving Enumerable#inject, Hash#merge with a block, and more and figure it’s time to ask for help.

UPDATE:

Figured I’d provide an update. This code is certainly not the solution but it might be heading in the right direction…

nodes = {
  :node1 => { :inherits => nil, :variables => { :foo => 1, :bar => 2 } },
  :node2 => { :inherits => :node1, :variables => { :foo => 9, :baz => 4 } }
}

new = nodes.inject({}) do |result, (k, v)|
  result.merge k => v.merge({ :variables => { :a => 6, :b => 7 } })
end

returns

{:node2=>{:inherits=>:node1, :variables=>{:a=>6, :b=>7}}, :node1=>{:inherits=>nil, :variables=>{:a=>6, :b=>7}}}

So that v.merge is not working as intended…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:45:56+00:00Added an answer on June 12, 2026 at 10:45 am
    merged_nodes = {}
    
    nodes.each do |name, node|
      merged_nodes[name] = node.dup
      merged_nodes[name][:variables] = if node[:inherits]
        nodes[node[:inherits]][:variables].merge node[:variables]
      else
        node[:variables].dup
      end
    end
    

    will give you

    {
      :node1=>{:inherits=>nil, :variables=>{:foo=>1, :bar=>2}},
      :node2=>{:inherits=>:node1, :variables=>{:foo=>9, :bar=>2, :baz=>4}}
    }
    

    but it won’t handle deeper nesting i.e. if :node3 then inherits :node2… if you need anything as complicated as that, this hash-based approach is going to get pretty clunky.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

fringe = new PriorityQueue<Node>(10,new Comparator<Node>(){ @Override public int compare(Node node1,Node node2) { if (f(node1)>f(node2))
What is wrong with the following line? <xsl:for-each select=//Node1/Node2/Node3 [.!='Primary' or .!='Secondary' ]> Trying
Given two Erlang nodes, foo@host and bar@host, the following produces a print-out on foo:
How to take distinct nodes list in XML in c# for example <root> <node1
@inherits umbraco.MacroEngines.DynamicNodeContext @using System.Collections; @{ List<string> qa = new List<string>(); } //this is not
I have been trying to create a Class for my OpenGL ES 2.0 +
@inherits umbraco.MacroEngines.DynamicNodeContext @using System @using System.Linq @using System.Xml.Linq @{ dynamic node = new umbraco.MacroEngines.DynamicNode(1121);
I am trying to inherit from TreeNode to add own nodes to TreeView: class
My configuration Cluster with two nodes: Node 1: Hardware: Intel Xeon 2.83 GHz (4
So I have two tables structured like so: CREATE TABLE #nodes(node int NOT NULL);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.