Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.45 KB | None | 0 0
  1. INPUT = [
  2.   {
  3.     'Name' => %w[
  4.       Dominik
  5.       Radzio
  6.       Bartek
  7.     ],
  8.     'Surname' => %w[
  9.       Duda
  10.       Pepek
  11.       Monka
  12.     ],
  13.     'Score' => %w[
  14.       10
  15.       6
  16.       1
  17.     ]
  18.   },
  19.   {
  20.     'Score' => [
  21.       '1 - 3',
  22.       '4 - 6',
  23.       '7 - 10'
  24.     ],
  25.     'Label' => %w[
  26.       Bad
  27.       Good
  28.       Excellent
  29.     ]
  30.   },
  31.   {
  32.     'X' => %w[
  33.       34
  34.       77
  35.     ],
  36.     'Y' => %w[
  37.       334
  38.       1
  39.     ],
  40.     'Z' => %w[
  41.       12
  42.       17
  43.     ]
  44.   },
  45.   {
  46.     'Krzys' => %w[
  47.       34
  48.       77
  49.       ale
  50.     ],
  51.     'Wojtek' => %w[
  52.       334
  53.       1
  54.       jestem
  55.     ],
  56.     'RyÅ›' => %w[
  57.       12
  58.       nom
  59.       glupi
  60.     ],
  61.     'Polikarp' => %w[
  62.       12
  63.       hx
  64.       hihi
  65.     ]
  66.   }
  67.  
  68. ].freeze
  69.  
  70. class Table
  71.   amount_tables = INPUT.each { |input|  input.keys.first }
  72.   amount = amount_tables.count
  73.   amount.times do |x|
  74.     length = INPUT[x].values.first.length
  75.     raise ArgumentError, 'not eql' unless INPUT[x].values.all? { |arr| arr.length == length }
  76.   end
  77.   # robie tabelke zeby sie wstawiala z calym htmlowskim syntaxem
  78.  
  79.   def draw_table
  80.     result = ''
  81.     amount_tables = INPUT.each { |input|  input.keys.first }
  82.     amount = amount_tables.count
  83.     amount.times do |x|
  84.     result << "#{"\s"*3}<table>\n"
  85.     result << "#{"\s"*5}<thead>\n"
  86.     result << "#{"\s"*6}<tr>\n"
  87.     INPUT[x].keys.each do |value|
  88.       result << "#{"\s"*8}<th>#{value}</th>\n"
  89.       end.join('')
  90.     result << "#{"\s"*6}</tr>\n"
  91.     result << "#{"\s"*5}</thead>\n"
  92.     result << "#{"\s"*5}<tbody>\n"
  93.     length = INPUT[x].values.first.count
  94.     length.times do |row|
  95.       result << "#{"\s"*7}<tr>\n"
  96.       INPUT[x].values.each do |values|
  97.         result << "#{"\s"*9}<td>#{values[row]}</td>\n"
  98.       end
  99.       result << "#{"\s"*7}</tr>\n"
  100.       end
  101.     result << "#{"\s"*5}</tbody>\n"
  102.     result << "#{"\s"*3}</table>\n"
  103.     result << "#{"\s"*3}<br>\n"
  104.   end
  105.     result
  106.   end
  107.  
  108.  
  109.   def table
  110.     f = File.new('index2.html', 'w+')
  111.     f.puts <<-END
  112.  
  113. <!DOCTYPE html>
  114. <html>
  115.  <body>
  116.    <style>
  117.      table {
  118.        font-family: arial, sans-serif;
  119.        border-collapse: collapse;
  120.        width: 100%;
  121.      }
  122.  
  123.      td, th {
  124.        border: 1px solid #dddddd;
  125.        text-align: left;
  126.        padding: 8px;
  127.      }
  128.  
  129.      tr:nth-child(even) {
  130.        background-color: #dddddd;
  131.      }
  132.    </style>
  133.  
  134. #{draw_table}
  135.  </body>
  136. </html>
  137.      END
  138.     f.close
  139.   end
  140.  
  141. end
  142. Table.new.table
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement