create a function to output an html file with the title and body content and an ordered list from the input array in ruby

main.rb
def generate_html(title, body_content, ordered_list)
  File.open("output.html", "w") do |file|
    file.puts "<!DOCTYPE html>"
    file.puts "<html>"
    file.puts "<head>"
    file.puts "<title>#{title}</title>"
    file.puts "</head>"
    file.puts "<body>"
    file.puts "<p>#{body_content}</p>"
    file.puts "<ol>"
    ordered_list.each do |item|
      file.puts "<li>#{item}</li>"
    end
    file.puts "</ol>"
    file.puts "</body>"
    file.puts "</html>"
  end
end
475 chars
19 lines

The function generate_html takes in three arguments title, body_content, and ordered_list. It then uses Ruby's File.open method to create a file output.html and writes the HTML code to the file based on the arguments passed in. The body content is wrapped in a paragraph tag and the ordered list is generated using a loop over the items in the provided array.

related categories

gistlibby LogSnag