I''m trying to get a flattened array of children (down to a certain depth) of a folder (which is using acts_as_tree). def recursive_find_depth(depth) if self.children.length > 0 and depth != 0 self.children.each do |child| puts child.name child.recursive_find_depth(depth -= 1) end end end At the moment, the function ''puts'' the right output, i.e. all the children under a certain folder to a certain depth. However, I completely stuck about how to get the finished array of children out of this function (this shouldn''t be hierarchal, just a flattened array). If anyone''s wondering what I''m using this for - it''s a webdav system (PROPFIND). Thanks in advance -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Richard Inham wrote:> I''m trying to get a flattened array of children (down to a certain > depth) of a folder (which is using acts_as_tree). > > def recursive_find_depth(depth) > if self.children.length > 0 and depth != 0 > self.children.each do |child| > puts child.name > child.recursive_find_depth(depth -= 1) > end > end > end > > At the moment, the function ''puts'' the right output, i.e. all the > children under a certain folder to a certain depth. However, I > completely stuck about how to get the finished array of children out of > this function (this shouldn''t be hierarchal, just a flattened array). If > anyone''s wondering what I''m using this for - it''s a webdav system > (PROPFIND). Thanks in advanceWill the Ruby Array.flatten method do what you''re after? http://www.rubycentral.com/ref/ref_c_array.html#flatten c. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Cayce Balara wrote:> Richard Inham wrote: >> I''m trying to get a flattened array of children (down to a certain >> depth) of a folder (which is using acts_as_tree). >> >> def recursive_find_depth(depth) >> if self.children.length > 0 and depth != 0 >> self.children.each do |child| >> puts child.name >> child.recursive_find_depth(depth -= 1) >> end >> end >> end >> >> At the moment, the function ''puts'' the right output, i.e. all the >> children under a certain folder to a certain depth. However, I >> completely stuck about how to get the finished array of children out of >> this function (this shouldn''t be hierarchal, just a flattened array). If >> anyone''s wondering what I''m using this for - it''s a webdav system >> (PROPFIND). Thanks in advance > > Will the Ruby Array.flatten method do what you''re after? > > http://www.rubycentral.com/ref/ref_c_array.html#flatten > > c.def recursive_find_depth(depth) if self.children.length > 0 and depth != 0 child_array ||= [] self.children.each do |child| child_array << child puts child_array.join('' ,'') child.recursive_find_depth(depth -= 1) end return child_array end end Nope. The function above returns the first folder, but not ''child_array'' (although it ''puts'' it fine). -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Nov-16 14:20 UTC
Re: recursively finding children
Hi -- On Thu, 16 Nov 2006, Richard Inham wrote:> > I''m trying to get a flattened array of children (down to a certain > depth) of a folder (which is using acts_as_tree). > > def recursive_find_depth(depth) > if self.children.length > 0 and depth != 0 > self.children.each do |child| > puts child.name > child.recursive_find_depth(depth -= 1) > end > end > end > > At the moment, the function ''puts'' the right output, i.e. all the > children under a certain folder to a certain depth. However, I > completely stuck about how to get the finished array of children out of > this function (this shouldn''t be hierarchal, just a flattened array). If > anyone''s wondering what I''m using this for - it''s a webdav system > (PROPFIND). Thanks in advanceThis is untested, but hopefully will either work or give you some ideas: def recursive_find_depth(depth, list=[]) unless depth.zero? children.each do |child| list.push(child.name) child.recursive_find_depth(depth-1, list) end end list end David -- David A. Black | dblack-0o/XNnkTkwhBDgjK7y7TUQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 16 November 2006 17:06, Richard Inham wrote:> Cayce Balara wrote: > > Richard Inham wrote: > >> I''m trying to get a flattened array of children (down to a certain > >> depth) of a folder (which is using acts_as_tree). > >> > >> def recursive_find_depth(depth) > >> if self.children.length > 0 and depth != 0 > >> self.children.each do |child| > >> puts child.name > >> child.recursive_find_depth(depth -= 1) > >> end > >> end > >> end > >> > >> At the moment, the function ''puts'' the right output, i.e. all the > >> children under a certain folder to a certain depth. However, I > >> completely stuck about how to get the finished array of children out of > >> this function (this shouldn''t be hierarchal, just a flattened array). If > >> anyone''s wondering what I''m using this for - it''s a webdav system > >> (PROPFIND). Thanks in advance > > > > Will the Ruby Array.flatten method do what you''re after? > > > > http://www.rubycentral.com/ref/ref_c_array.html#flatten > > > > c. > > def recursive_find_depth(depth) > if self.children.length > 0 and depth != 0 > child_array ||= [] > self.children.each do |child| > child_array << child > puts child_array.join('' ,'') > child.recursive_find_depth(depth -= 1) > end > return child_array > end > end > > Nope. The function above returns the first folder, but not ''child_array'' > (although it ''puts'' it fine).def nflatten(depth, result = []) return [] if depth <= 0 self.inject([]) { |result, item| result << item << nflatten(depth-1, item); result } end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> This is untested, but hopefully will either work or give you some > ideas:Thanks David, works fine! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---