On Wed, Dec 21, 2011 at 14:56, Muhammet S. AYDIN <mengukagan / gmail.com> wrote: > - how can i make this better performing and rubyish? First, when you do "if node.children && node.children.size > 0", I'm not sure if that's all needed. Without seeing the definition, I'm not sure if a node with no children has children being nil, or an empty array. If a childless node has children as nil, then BTW your first check is nicely idiomatic Ruby (as opposed to "node.children != nil" or "! node.children.nil?"). However in that case, checking the array size may be redundant. On the other claw, if a childless node has an empty array for children, then checking if it exists is redundant. Or is it nil if it's never been initialized, but could be filled with children which could then get removed, leaving an empty array? Also, adding onto a string is generally not very performant. I don't know offhand if this is the case in Ruby, but in many languages you wind up creating a whole new object. So instead of tacking onto the string, push onto an array, and then join it all together at the end. You can even do it all in one swell foop by having the sub-invocations return an array, something like: def print_children(nodes) get_children(nodes).join end def get_children(nodes) retval = ["<ul>"] nodes.each do |node| retval.push "<li id=\"page_#{node.id}\"><a href=\"/documents/#{node.document_id}/pages/#{node.id}\">#{node.name}</a>" if node.children && node.children.size > 0 retval.concat get_children(node.children) end retval.push "</li>" end retval.push "</ul>" end Give that a whirl and let us know if it improves the speed. It might not be enough to really matter. As for making it more Rubyish, note the removal of "return retval" -- you don't need it, since in Ruby the return value of a function is the value of the last expression evaluated, and push has the new value of the array. -Dave -- LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern languages. Where: Northern Virginia, Washington DC (near Orange Line), and remote work. See: davearonson.com (main) * codosaur.us (code) * dare2xl.com (excellence). Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (Aronson)