Created
July 13, 2016 21:05
-
-
Save DiegoSalazar/d5ce9cf6dd03909e3b433f20c618a256 to your computer and use it in GitHub Desktop.
convert a hash to html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def convert_hash_to_html(data) | |
html = "<dl>" | |
data.each do |key, val| | |
html << "<dt>#{key}</dt>" | |
if val.is_a? Hash | |
html << "<dd>#{convert_hash_to_html val}</dd>" | |
elsif val.is_a? Array | |
html << "<dd>#{convert_array_to_html val}</dd>" | |
else | |
html << "<dd>#{val}</dd>" | |
end | |
end | |
html << "</dl>" | |
end | |
def convert_array_to_html(data) | |
html = "<dl>" | |
data.each do |val| | |
if val.is_a? Hash | |
html << "<dd>#{convert_hash_to_html val}</dd>" | |
elsif val.is_a? Array | |
html << "<dd>#{convert_array_to_html val}</dd>" | |
else | |
html << "<dd>#{val}</dd>" | |
end | |
end | |
html << "</dl>" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment