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

  • SEARCH
  • Home
  • 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 6203707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:57:44+00:00 2026-05-24T04:57:44+00:00

class Tree attr_accessor :children, :node_name def initialize(tree_hash={}) tree_hash.each do |k,v| @node_name = k @children

  • 0
class Tree
attr_accessor :children, :node_name

def initialize(tree_hash={})
    tree_hash.each do |k,v|
        @node_name = k
        @children = v.map { |k,v|   Tree.new(k,v) }
    end
end

def visit_all(&block)
    visit &block
    children.each {|c| c.visit_all &block}
end

def visit(&block)
    block.call self
end
end

ruby_tree = Tree.new({'grandpa' => {'dad' => {'child 1' => {}, 'child 2' => {} },'uncle' => {'child 3' => {}, 'child 4' => {} } } })

puts "Visiting a node"
ruby_tree.visit {|node| puts node.node_name}
puts

puts "visiting entire tree"
ruby_tree.visit_all {|node| puts node.node_name}

And the error message: (I am using ruby 1.9.2)

C:\ruby>ruby tree.rb

tree.rb:4:in `initialize’: wrong number of arguments (2 for 1) (ArgumentError)

   from tree.rb:7:in `new'
   from tree.rb:7:in `block (2 levels) in initialize'
   from tree.rb:7:in `each'
   from tree.rb:7:in `map'
   from tree.rb:7:in `block in initialize'
   from tree.rb:5:in `each'
   from tree.rb:5:in `initialize'
   from tree.rb:21:in `new'
   from tree.rb:21:in `<main>'
  • 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-05-24T04:57:45+00:00Added an answer on May 24, 2026 at 4:57 am

    There are 2 args in the line:

    @children = v.map { |k,v|   Tree.new(k,v) }
    

    Maybe you want to change k,v to {k => v} ?

        @children = v.map { |k,v|   Tree.new(k=>v) }
    

    I think the initialisation is problematic though- what kind of tree are you implementing?

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

Sidebar

Related Questions

class Tree def initialize*d;@d,=d;end def to_s;@l||@r?,>:@d;end def total;(@d.is_a?(Numeric)?@d:0)+(@l?@l.total: 0)+(@r?@r.total: 0);end def insert d alias
I can't seem to get this right. class Tree { Node* root; vector& dict;
How would I write a map function for an n-ary tree in ruby? class
I have this very simple C++ class: class Tree { public: Node *head; };
Some text before the code so that the question summary isn't mangled. class Tree
I have a business object project, which contains composite structure: public class Tree {
I have a binary tree class that is created with a root node and
I'm writing an immutable binary tree class where all of the methods (Insert, Remove,
I would like to extend a kd-tree (2D) class to be able to remove
I have tree control object created using CTreeCtrl MFC class. The tree control needs

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.