Created
October 5, 2011 06:59
-
-
Save minhajuddin/1263821 to your computer and use it in GitHub Desktop.
common ruby extensions
This file contains hidden or 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
| 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