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 5948737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:09:03+00:00 2026-05-22T17:09:03+00:00

Excuse I am newbie in ruby. My problem is about argument passes by value

  • 0

Excuse I am newbie in ruby.

My problem is about argument passes by value and reference.

I am coding this method

def show_as_tree(parents)
  array = []
  iterate_categories(parents, array)
end

def iterate_categories(parents, array)
  parents.each do |p|
    #return p.description or "-#{p.description} if the node is root or not
    p.description = category_name(p)

    #add to array
    array << p

    #call iterate categories with children of parent node and same array
    iterate_categories(p.children, array)
  end
end

however the array content is only the parent nodes.

I need understand the ruby mechanism for references and how could fix my problem?

  • 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-22T17:09:04+00:00Added an answer on May 22, 2026 at 5:09 pm

    I’m pretty sure Ruby just creates a copy of your array. Therefore what you should be doing is having

    array << iterate_categories(p.children, new_array)
    

    And return your array at the end of the function.

    Just did a quick example: (Updating code based on Wayne Conrad’s answer… it is correct)

    class Person
      attr_accessor :name, :children
    end
    
    class Test
    
      def iterate_categories(parents,array)
        parents.each do |p|
          array << p.name
          if !p.children.nil?
            iterate_categories(p.children,array)
          end
        end
      end
    
      def iterate_categories_test
        p1 = Person.new
        p1.name = "Bob"
    
        p2 = Person.new
        p2.name = "Joe"
    
        p3 = Person.new
        p3.name = "Ann"
    
        p4 = Person.new
        p4.name = "John"
    
        p1.children = [p2,p3]
        p3.children = [p4]
    
        array = []
        iterate_categories([p1],array)
        puts array
      end
    end
    

    Then:

    >> a = Test.new
    >> a.iterate_categories_test
    Bob
    Joe
    Ann
    John
    => nil
    

    Hopefully that helps.

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

Sidebar

Related Questions

I am a patterns newbie so please excuse this question if it sounds too
I am a newbie in this field so please excuse my silly mistakes :)
I am kind of a newbie so excuse me if this question is too
Excuse me if this is slightly newbie. I have the main view located @
I'm a complete newbie to C# so excuse me if this looks weird. I
Please excuse the newbie code, im new. Any coding tips are much appreciated as
I'm working on learning some Haskell (please excuse the newbie error)- This routine errors
im a newbie to flash,and i hav got this really weird problem.i have created
Excuse the newbie question. I'm trying to create a two dimensional array in ruby,
I am a newbie to PHP still, so excuse this question if it seems

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.