Skip to content

Instantly share code, notes, and snippets.

@TimLang
Created October 31, 2013 08:41
Show Gist options
  • Save TimLang/7246194 to your computer and use it in GitHub Desktop.
Save TimLang/7246194 to your computer and use it in GitHub Desktop.
Array to table format
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