Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To generate a table using Ruby, you can use the built-in Ruby method called "puts" to print out the elements of the table in a formatted manner. Here is an example code snippet:

table = [
  ["Name", "Age", "Gender"],
  ["John", 23, "Male"],
  ["Jane", 26, "Female"],
  ["Bob", 35, "Male"]
]

table.each do |row|
  row.each do |column|
    print column.to_s.ljust(10)
  end
  puts ""
end

In this example, we first define the table as an array of arrays, with each inner array being a row in the table. We then use a nested loop to iterate through each element in the table and print it out using the print method. We use the ljust method to left-align each column and ensure that they are all the same width. Finally, we use the puts method to print out each row on a new line.