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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:10:38+00:00 2026-06-07T04:10:38+00:00

I have an object, tree . tree has a property, tree.elements . tree.elements is

  • 0

I have an object, tree. tree has a property, tree.elements. tree.elementsis an array containing both elements and possibly other sub-trees, which in turn will have their own elements array, and so on.

I need a method which will be able to replace objects in the tree if they are of a certain class. The problem is replacing elements inline.

Obviously, the following will not work:

[1,2,3].each { |n| n = 1 }
# => [1,2,3]

But, this will:

a = [1,2,3]
a.each_with_index { |n, idx| a[idx] = 1 }
# => [1,1,1]

However, I am using a recursive function to loop through, and replacing placeholders with content, like so:

def replace_placeholders(elements)
    elements.each do |e|
        if e.respond_to?(:elements) and e.elements.any?
            replace_placeholders(e.elements)
        elsif e.is_a? Placeholder
            e = "some new content" # << replace it here
        end
    end
end

Keeping track of the indices is really complicated. I’ve tried e.replace("some new content"), but that doesn’t work. What’s the best way to go about this?

  • 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-07T04:10:39+00:00Added an answer on June 7, 2026 at 4:10 am

    I would create a new array rather than trying to update in-place. Something along these lines should work:

    def replace_placeholders(elements)
      elements.map do |e|
        if e.respond_to?(:elements) and e.elements.any?
          e.elements = replace_placeholders(e.elements) # replace array
          e  # return e itself, so that map works correctly.
        elsif e.is_a? Placeholder
          "some new content"
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tree-like object, managed by Hibernate. The object has a list of
I have a business object project, which contains composite structure: public class Tree {
I have an object tree which is included in a ResourceDictionary in my application.
I have a NSTreeController which array is bound to a items (custom) property of
I have a complex object(tree structure) which I am flattening it out into a
If I have a form-backing object that has a complicated object tree -- say
I have XElement object which is my XML tree read from XML file. Now
I am trying to have a javascript object tree behave like a php associative
I have an own Tree object implemented in PHP. Say we have a tree
I have tree like following example, where every leaf is object. [1] |--[2] |

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.