Reputation: 1
I'm currently working through a set of TestFirst problems. The spec for the problem I'm working on can be found here: http://testfirst.org/live/learn_ruby/book_titles
I've tested the title method outside of the class so I know that it works, but once I place it in the class I get the following error:
1) Book title should capitalize the first letter
Failure/Error: @book.title.should == "Inferno"
ArgumentError:
wrong number of arguments (0 for 1)
Here's what I have so far:
class Book
attr_accessor :title
def initialize(title=nil)
@title = title
end
def title(title)
first_check = 0
articles = %w{a the an in and of}
words = title.split(" ")
words.each do |word|
if first_check == 0
word.capitalize!
first_check += 1
elsif !articles.include?(word)
word.capitalize!
end
end
@title = words.join(" ")
end
end
If someone could explain how the class should be formatted, it'd be greatly appreciated!
Upvotes: 0
Views: 102
Reputation: 434595
Your problem is right here:
def title(title)
Your Book#title
method expects an argument but you're not giving it one in your spec:
@book.title.should == "Inferno"
# ----^^^^^ this method call needs an argument
I think you actually want a Book#title=
method:
def title=(title)
# The same method body as you already have
end
Then you'll use the title
accessor method that attr_accessor :title
supplies and assigning a new title will use your title=
method. And since you're supplying your own mutator method, you could use attr_reader
instead:
class Book
attr_reader :title
def initialize(title=nil)
@title = title
end
def title=(title)
#...
end
end
Upvotes: 1