Created
August 4, 2013 13:02
-
-
Save baisa/6150273 to your computer and use it in GitHub Desktop.
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
array = [['Number', 'Name'], [1, 'Foo'], [2, 'Bar'], [3, 'Baz']] | |
def pretty_table(array) | |
new_array = array.collect {|elem| | |
elem.collect! {|el| el.to_s} | |
elem.max_by {|el| el.length} | |
} | |
m = new_array.max_by {|elem| elem.length} | |
i = 0 | |
z = "|" + "#{array[i][0]}".ljust(m.size) + " | " + "#{array[i][1]}".ljust(m.size) + " | " | |
w = z.length | |
puts w | |
string = "" | |
array.each do |elem| | |
string << "-" * w | |
string << "\n" | |
string << "|" + "#{array[i][0]}".ljust(m.size) + " | " + "#{array[i][1]}".ljust(m.size) + " | " | |
string << "\n" | |
i = i + 1 | |
end | |
string | |
end | |
print pretty_table(array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment