Created
October 31, 2013 08:41
-
-
Save TimLang/7246194 to your computer and use it in GitHub Desktop.
Array to table format
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 Array | |
| def to_table_format | |
| return self unless self.grep(/\${image\d+}/).empty? | |
| cached_max = {} | |
| unless self[0].class == Array | |
| cached_max[0] = self.map(&:size).max | |
| else | |
| self[0].size.times.each do |i| | |
| cached_max.merge!(i => self.map{|a| a[i]}.map(&:size).max) | |
| end | |
| end | |
| to_do = lambda do |obj, index| | |
| ((obj == "\n") || (obj == "( )")) ? obj : (obj + ("-" * (cached_max[index] - obj.size))) | |
| end | |
| self.map{|a| a.is_a?(Array) ? a.each_with_index.map{|a, i| to_do.call(a, i)} : to_do.call(a, 0)} | |
| end | |
| end | |
| a = [["aaaaa", "dsdsdsdsd", "A", "xxxxxx"], ["xxx", "dsdsddsdsdsdsdsdsdsdsd", "B", "s"]] | |
| #a = ["aaa", "vvvvv", "ccccc"] | |
| puts a.to_table_format |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment