Skip to content

Instantly share code, notes, and snippets.

@minhajuddin
Created October 5, 2011 06:59
Show Gist options
  • Select an option

  • Save minhajuddin/1263821 to your computer and use it in GitHub Desktop.

Select an option

Save minhajuddin/1263821 to your computer and use it in GitHub Desktop.
common ruby extensions
class Hash
#example:
# emp = {:name => "Khaja Minhajuddin", :dob => {:day => 26, :month => "January", :year => 1985}}
# puts emp.to_html_table
# => "<table><tr><td>name</td><td>Khaja Minhajuddin</td></tr><tr><td>dob</td><td><table><tr><td>day</td><td>26</td></tr><tr><td>month</td><td>January</td></tr><tr><td>year</td><td>1985</td></tr></table></td></tr></table>"
def to_html_table
output_html = "<table>"
self.each do |key, value|
value_html = value.is_a?(Hash) ? value.to_html_table : value
output_html << "<tr><td>#{key}</td><td>#{ value_html }</td></tr>"
end
output_html << "</table>"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment