2

Given the following code:

  class Book
    attr_accessor :author
    attr_reader :title
    attr_writer :comments
    def initialize(author, title)
      @author = author
      @title = title
      @comments = []
    end
  end

  book = Book.new("Chuck Palahniuk", "Fight Club")

Which of the following snippets of code are valid?

1."#{book.title} was written by #{book.author}."

2.book.comments << "#{book.title} was a good book"

book.comments.each { |comment| puts comment }

book.title = "Cooking Club"

4

1 回答 1

4

答案是#1 ...我如何获得我的巧克力?

#2 - There is no attr_accessor or attr_writer for comments
#3 - There is no attr_accessor or attr_reader for comments
#4 - There is no attr_accessor or attr_writer for title
于 2012-10-08T19:19:19.517 回答