Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.