Please tell me what am I doing wrong here. I want to be able to capitalize any string that is being added later in the code through the method titles .
class Book
attr_accessor :book_title
def initialize
@book_title = String.new
end
def titles
book_title.capitalize
end
end
@book = Book.new
puts @book.titles = "steve jobs"
Because when I execute it, I get undefined method `titles=' for #<Book:0x007fbd25813d98 @book_title=""> (NoMethodError)
It’s a little unclear as to what you’re trying to accomplish. You’re adding an
attr_accessorforbook_title, not fortitles(which isn’t a variable at all). You need to callin order to set (and print) the title.
If you’re trying to pass a title to
titlesand have that method capitalize the title and set@book_titleto that, you need to declare it as an assignment method using=, pass in the parametertitle, and actually set@book_titleto that. Something like thisCurrently your
titlesmethod only returns the capitalized local variablebook_title, which doesn’t exist (it needs the@to reference the instance variable).