[LRUG] Code Help

Andrew Stewart boss at airbladesoftware.com
Fri Feb 29 09:47:24 PST 2008


On 29 Feb 2008, at 14:29, Anthony Green wrote:
> Can anyone suggest any improvements to this code
>
> def tree_to_nested_list(object, &block)
>  content_tag(:ol) do
>   content_tag(:li) do
>    html = yield object if block_given?
>    html += object.children.map { |child| tree_to_nested_list(child,  
> &block)
> }.join('\n')
>    end
>  end
> end

How about this?

def tree_to_nested_list(object, &block)
   content_tag(:ol) do
     content_tag(:li) do
       html = block_given? ? yield(object) : ''
       object.children.inject(html) do |html, child|
         html << tree_to_nested_list(child, &block)
       end
    end
end

N.B. This is untested :-)  And may not be any improvement.

Regards,
Andy Stewart

-------
http://airbladesoftware.com






More information about the Chat mailing list