Skip to content

Instantly share code, notes, and snippets.

@chastell
Created September 6, 2011 07:01
Show Gist options
  • Save chastell/1196800 to your computer and use it in GitHub Desktop.
Save chastell/1196800 to your computer and use it in GitHub Desktop.
class Hash
def to_html
[
'<ul>',
map { |k, v| ["<li><strong>#{k}</strong>", v.respond_to?(:to_html) ? v.to_html : "<span>#{v}</span></li>"] },
'</ul>'
].join
end
end
# h = {a: 'A', b: { c: 'C', d: { e: 'E', f: 'F'}, g: 'G'}, h: 'H' }
# h.to_html
# => "<ul><li><strong>a</strong><span>A</span></li><li><strong>b</strong><ul><li><strong>c</strong><span>C</span></li><li><strong>d</strong><ul><li><strong>e</strong><span>E</span></li><li><strong>f</strong><span>F</span></li></ul><li><strong>g</strong><span>G</span></li></ul><li><strong>h</strong><span>H</span></li></ul>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment