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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:21:41+00:00 2026-06-13T12:21:41+00:00

I am trying to write a function to convert a flat array with a

  • 0

I am trying to write a function to convert a flat array with a path information into a tree representation of that array.

The goal would be to turn an array like the following:

[
{ :name => "a", :path => [ 'a' ] },
{ :name => "b", :path => [ 'a', 'b' ] },
{ :name => "c", :path => [ 'a', 'b', 'c' ] },
{ :name => "d", :path => [ 'a', 'd' ] },
{ :name => "e", :path => [ 'e' ] }
]

into one like this:

[{:node=>{:name=>"a", :path=>["a"]},
  :children=>
   [{:node=>{:name=>"b", :path=>["a", "b"]},
     :children=>
      [{:node=>{:name=>"c", :path=>["a", "b", "c"]}, :children=>[]}]},
    {:node=>{:name=>"d", :path=>["a", "d"]}, :children=>[]}]},
 {:node=>{:name=>"e", :path=>["e"]}, :children=>[]}]

The closest result I got with was with the following code:

class Tree

  def initialize
    @root = { :node => nil, :children => [ ] } 
  end 

  def from_array( array )
    array.inject(self) { |tree, node| tree.add(node) }
    @root[:children]
  end 

  def add(node)
    recursive_add(@root, node[:path].dup, node)
    self
  end 

  private

  def recursive_add(parent, path, node)
    if(path.empty?)
      parent[:node] = node
      return
    end 
    current_path = path.shift
    children_nodes = parent[:children].find { |child| child[:node][:path].last == current_path } 
    unless children_nodes
      children_nodes = { :node => nil, :children => [ ] } 
      parent[:children].push children_nodes
    end 
    recursive_add(children_nodes, path, node)
  end 
end

flat = [ 
  { :name => "a", :path => [ 'a' ] },
  { :name => "b", :path => [ 'a', 'b' ] },
  { :name => "c", :path => [ 'a', 'b', 'c' ] },
  { :name => "d", :path => [ 'a', 'd' ] },
  { :name => "e", :path => [ 'e' ] } 
]

require 'pp'
pp Tree.new.from_array( flat )

But it is quite verbose and I have the feeling that it might not be very effective for very large sets.

What would be the cleanest and most effective way to achieve that in ruby?

  • 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-13T12:21:42+00:00Added an answer on June 13, 2026 at 12:21 pm

    This is my try.

    array = [
    { :name => "a", :path => [ 'a' ] },
    { :name => "b", :path => [ 'a', 'b' ] },
    { :name => "c", :path => [ 'a', 'b', 'c' ] },
    { :name => "d", :path => [ 'a', 'd' ] },
    { :name => "e", :path => [ 'e' ] }
    ]
    
    array
    .sort_by{|h| -h[:path].length}
    .map{|h| {node: h, children: []}}
    .tap{|array| 
      while array.first[:node][:path].length > 1
        child = array.shift
        array
        .find{|h| h[:node][:name] == child[:node][:path][-2]}[:children]
        .push(child)
      end
    }
    
    # => [
      {:node=>{:name=>"e", :path=>["e"]}, :children=>[]},
      {:node=>{:name=>"a", :path=>["a"]}, :children=>[
        {:node=>{:name=>"d", :path=>["a", "d"]}, :children=>[]},
        {:node=>{:name=>"b", :path=>["a", "b"]}, :children=>[
          {:node=>{:name=>"c", :path=>["a", "b", "c"]}, :children=>[]}
        ]}
      ]}
    ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function to convert a python list into a JSON
I am trying to write a function that will convert a generic list to
I'm trying to write a function to convert a time & date into epoch
I'm trying to write a function that'll convert an integer to a string like
Im trying to write a function that will convert all charactewrs after the first
I am trying to write a function that will convert a DateTime.Now instance to
I am trying write a function that generates simulated data but if the simulated
I am trying to write a function that will allow me to browse for
I am trying to write a function that can take three parameters: an SqlDataReader,
I am trying to write a function to parse the string representation of a

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.