Reputation: 133
I'm a beginner and I've tried looking through all my online resources. I can't figure out what I'm doing wrong even though I'm adding the "@" symbols so that the 'title' and 'author' variable applies the the second method (description.) Not sure why running this code comes up with a NameError - undefined local variable or method `title' for #. Any ideas? Thank you
class Book
def set_title_and_author(title,author)
@title = title
@author = author
end
def description
puts "#{title} was written by #{author}"
end
end
here are some specs:
describe "Book" do
describe "description" do
it "should return title and author in description" do
book = Book.new
book.set_title_and_author("Ender's Game","Orson Scott Card")
expect( book.description ).to eq("Ender's Game was written by Orson Scott Card")
end
end
end
Upvotes: 1
Views: 69
Reputation: 24347
You need to use the @ when setting and when getting an instance variable:
class Book
def set_title_and_author(title, author)
@title = title
@author = author
end
def description
puts "#{@title} was written by #{@author}"
end
end
Upvotes: 2