Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def convert_to_list item
- if item.kind_of? Array then
- "<ul>#{item.map {|part| convert_to_list part }.join }</ul>"
- elsif item.kind_of? Hash then
- "<dl>#{item.map {|k, v| "<dt>#{k}</dt><dd>#{convert_to_list(v)}</dd>"}.join}</dl>"
- else
- "<li>#{item}</li>"
- end
- end
- def convert_to_list_2 item
- if item.kind_of? Array then
- "<ul>#{item.map {|part| convert_to_list_2 part }}</ul>"
- elsif item.kind_of? Hash then
- "<dl>#{item.map {|k, v| "<dt>#{k}</dt><dd>#{convert_to_list_2(v)}</dd>"}}</dl>"
- else
- "<li>#{item}</li>"
- end
- end
- list = [1, 2, [3, 6], 5]
- p convert_to_list(list)
- list = [1, 2, [3, 6], 5, {:a => :b}]
- p convert_to_list(list)
- p convert_to_list_2(list)
- list = [1, 2, [3, ['a', 'b']], 5, {:a => :b}]
- p convert_to_list(list)
- list = [1, 2, [3, []], 5, {:a => :b}]
- p convert_to_list(list)
Add Comment
Please, Sign In to add comment