Ruby-在使用类和方法的简单ToDo列表程序时遇到问题

Ruby-在使用类和方法的简单ToDo列表程序时遇到问题,ruby,Ruby,我想做一个小而简单的待办事项列表程序。我在这件事上已经空白了好几个小时了。以下是说明: > # Write a program that keeps a list of todos. It should: > # * Have two classes: > # * `Todo` > # * Has two methods: > # * `initialize`: Creates a new todo object. Takes a strin

我想做一个小而简单的待办事项列表程序。我在这件事上已经空白了好几个小时了。以下是说明:

> # Write a program that keeps a list of todos. It should:
> # * Have two classes:
> #   * `Todo`
> #     * Has two methods:
> #       * `initialize`: Creates a new todo object. Takes a string as a
> #         parameter and stores it on the todo object.
> 
> #       * `text`: Takes no parameters. Returns the string that was
> #         stored when the todo object was created.
> 
>  
> #   * `TodoList`
> #     * Has three methods:
> #       * `initialize`: Creates a new todo list object. Takes no parameters.
> 
> #       * `add`: Takes a todo object as a parameter.  Stores it on the
> #         todo list object.
> 
> #       * `print`: Takes no parameters.  Creates a string of all the
> #         stored todos, one per line.  Each line should start with a
> #         `* `.  `puts`es the string.
> 
> #         * e.g.
> #           ```
> #           * get milk
> #           * get the papers
> #           ```
以下是我对程序代码的尝试:

class Todo
  def initialize(todo)
    @todo = todo
  end

  def text
    @todo
  end
end
class TodoList
  def initialize
    @todo_list = []
  end

  def add(todo)
    @todo_list << Todo.new(todo)
  end

  def print
    @todo_list.each do |x|
      puts "* #{x}"
    end
  end
end
类待办事项
def初始化(todo)
@待办事项
结束
定义文本
@待办事项
结束
结束
托多利斯特阶级
def初始化
@待办事项列表=[]
结束
def添加(todo)

@待办事项清单对于正在发生的事情,您有点崩溃

class Todo
  def initialize(todo)
    @todo = todo   # This sets your instance variable @todo to whatever you passing, you will probably want to add a check to make sure you only accept strings
  end

  def text
    @todo  # The text method is returning whatever the @todo variable is, in this case since you are passing a string, it is returning a string
  end
end

class TodoList
  def initialize
    @todo_list = []  # When you are initializing your TodoList you are creating an array called @todo_list
  end

  def add(todo)
    @todo_list << Todo.new(todo) # When you call this method you are adding a new instance of your Todo class
  end

  def print
    # Here you are iterating through your array @todo_list, which we established earlier is an array of your Todo Class variables
    @todo_list.each do |x| # Pro tip, try to stay away from using names like 'x' or 'y' use more descriptive names
      # Your 'x' variable here is one of your Todo classes instances
      puts "* #{x.text}" # As defined in your Todo class, you will need to call the text method to retrieve the text you stored in your class
    end
  end
end

list = TodoList.new  # Here we create a new instance of your TodoList class

list.add('Buy Pizza') # Here we can add items onto your todo list
list.add('Buy Pie')
list.add('Buy Potato')
list.print  # This is how you will print all the items you added to your TodoList

关于正在发生的事情,你有点崩溃

class Todo
  def initialize(todo)
    @todo = todo   # This sets your instance variable @todo to whatever you passing, you will probably want to add a check to make sure you only accept strings
  end

  def text
    @todo  # The text method is returning whatever the @todo variable is, in this case since you are passing a string, it is returning a string
  end
end

class TodoList
  def initialize
    @todo_list = []  # When you are initializing your TodoList you are creating an array called @todo_list
  end

  def add(todo)
    @todo_list << Todo.new(todo) # When you call this method you are adding a new instance of your Todo class
  end

  def print
    # Here you are iterating through your array @todo_list, which we established earlier is an array of your Todo Class variables
    @todo_list.each do |x| # Pro tip, try to stay away from using names like 'x' or 'y' use more descriptive names
      # Your 'x' variable here is one of your Todo classes instances
      puts "* #{x.text}" # As defined in your Todo class, you will need to call the text method to retrieve the text you stored in your class
    end
  end
end

list = TodoList.new  # Here we create a new instance of your TodoList class

list.add('Buy Pizza') # Here we can add items onto your todo list
list.add('Buy Pie')
list.add('Buy Potato')
list.print  # This is how you will print all the items you added to your TodoList

您的打印方法有错误。它应该是“*#{x.text}”,当您编写代码时,运行它并查看输出非常有用。。。在这种情况下,您会清楚地看到puts语句没有输出文本提示:为
Todo
创建一个
to\s
方法,将其转换为格式良好的字符串。@chemical,谢谢,但没有。事实上,这仍然不能解决我遇到的问题,以上3条建议仍然返回相同的打印错误消息您的打印方法有错误。它应该是“*#{x.text}”,当您编写代码时,运行它并查看输出非常有用。。。在这种情况下,您会清楚地看到puts语句没有输出文本提示:为
Todo
创建一个
to_s
方法,将其转换为格式良好的字符串。@chemical,谢谢,但这仍然不能解决我遇到的问题事实上,上述3个建议仍然会返回相同的打印错误消息