user3706202
user3706202

Reputation: 215

What's causing this no method error?

Ive prepared this code. Aim is simple -> add a new book and then call the description method which should product "Title is written by author"

But when I run it I get

NoMethodError: undefined method `set_title_and_author' for #

Whats going wrong?

class Book
 def set_title_and_author=(title, author)
   @title = title
   @author = author
 end

 def description
   print "#{title} is wrtten by #{author}" 
 end
 end

book = Book.new
book.set_title_and_author("The hunger games", "Larry Page")
book.description

Upvotes: 0

Views: 100

Answers (1)

Doon
Doon

Reputation: 20232

well you are overriding the = operator, but then not calling it..

def set_title_and_author(title, author)
   @title = title
   @author = author
 end

is probably what you want.

normally you would use the = method to build a customer attr_writer. such as

  def author=(author)
     @author = author.capitalize
  end

or something similar (forced example above)

Upvotes: 2

Related Questions