0
class Books
    attr_accessor :name, :book_id
    def initialize(name, book_id)
        @name = name,
        @book_id = book_id
    end
end

class BookCollection
    def intialize
        @book_names = []
    end

    def add_to_books(book_name)
        book_name.push(book_names)
    end
end

book1 = Books.new("catch22", "12345")
book_collection1 = BookCollection.new
book_collection1.add_to_books(book1.name)
puts book_collection1

end

That is my code and the error I'm getting is "undefined local variable or method `book_names'". I tried adding " attr_accessor :book_names" and when I do that the printed output doesn't make sense.

2
  • Don't you mean @book_names.push(book_name)? Also, instead of puts book_collection you need a method (e.g., attr_reader) to return @book_names. Commented Nov 23, 2013 at 3:35
  • If your question was answered, please select an answer. Commented Nov 27, 2013 at 5:17

2 Answers 2

1

There are a few mistakes in your code:

  • line 4 should not end with a comma.
  • initialize in class BookCollection is misspelled, resulting in @book_names not being initialized. @book_names therefore equals nil when you attempt to add an element to it with push. nil does not have a method push; hence the exception, and the message printed with the exception.
  • book_name.push(book_names) should be @book_name.push(book_name). (@book_name must be an instance_variable, as opposed to a local variable, to be visible outside a method, within the class definition.
  • puts book_collection1 prints the class instance; you want to print @book_names.

Here I've fixed your code. I've used << instead of push. Either is OK, but the former seems to be favored my most.

class Books
  attr_accessor :name, :book_id
  def initialize(name, book_id)
  puts "name = #{name}, book_id = #{book_id}"
    @name = name
    @book_id = book_id
  end
end

class BookCollection
  attr :book_names
  def initialize
    @book_names = []
  end    
  def add_to_books(book_name)
    @book_names << book_name
  end
end

book_collection1 = BookCollection.new
book1 = Books.new("Catch22", "12345")
book2 = Books.new("Hawaii", "67890")
book_collection1.add_to_books(book1.name)
book_collection1.add_to_books(book2.name)

book_collection1.book_names # => ["Catch22", "Hawaii"]
Sign up to request clarification or add additional context in comments.

Comments

0

Probably just a typo at

book_name.push(book_names)

Should have been

book_names.push(book_name)

With attr_accessor :book_names

3 Comments

You forgot to slap a @ on the front of book_names.
I made all the changes and still get "add_to_books': undefined method push'". Btw, why is the @ necessary? I was told before that you didn't need it. @CarySwoveland
Right, there was no attr_accessor for book_names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.