简体   繁体   中英

How do I use the Ruby attributes template, shop_items, and date to do the required tasks?

Help is greatly appreciated, as I'm confused on how to use the attributes. 在此处输入图像描述

require 'erb'

class ShoppingList
  attr_reader :template
  attr_reader :shopping_items
  attr_reader :date

  def initialize(shopping_items, template, date = Time.now)
    @template = template
    @shopping_items = shopping_items
    @date = date
  end

  def output(output_file)
    File.write(output_file, render)
  end

  private

  def render
    ERB.new(@template).result(binding)
  end
end

def shopping_items
  ["milk", "egg", "ham", "bread"]
end

def template
  %{
    <html>
    <head></head>
    <body>
    <h1> Shopping List for <%= @date.strftime('%A, %d, %B, %Y')%></h1>
    <p> You need to buy </p>
    <ul>
      <% @shopping_items.each do |item| %>
        <li><%= item %></li>
      <% end %>
    </ul>
    </doby>
    </html>
  }
end

ShoppingList.new(shopping_items, template).output('shopping_list.html')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM