Created
December 18, 2019 15:24
-
-
Save MatthewRDodds/a628168db9e7c37aeda4e01142c8c12b to your computer and use it in GitHub Desktop.
Markdown Table Generator Script
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
# | |
# Usage: | |
# | |
# rows = [['User ID', 'Email'], ['1', '[email protected]'],[2,'[email protected]']] | |
# MarkdownTableGenerator.generate(rows) | |
# | |
module MarkdownTableGenerator | |
extend self | |
PIPE = '|'.freeze | |
DASH = '-'.freeze | |
def pipify(row_values) | |
row_values.join(PIPE).prepend(PIPE).concat(PIPE) | |
end | |
def generate(rows) | |
header_row = rows.shift | |
puts <<~EOS | |
#{pipify(header_row)} | |
#{pipify((DASH * header_row.size).split(''))} | |
#{rows.map { |row| pipify(row) }.join("\n")} | |
EOS | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment