Issue properly formatting ruby to pass rspec - ruby

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!

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

Related

Book Capitalization Class Error

I'm teaching myself how to code and can't seem to understand how the inputted title is being called by this class. Where does the argument go to first? The attr_accessor or the def title capital_it(#title) end?
class Book
attr_accessor :title
def title
capital_it(#title)
end
def capital_it(title)
word_arr = #title.capitalize.split(" ")
word_arr.map do |word|
word.capitalize! unless little_words.include?(word)
end
word_arr.join(" ")
end
def little_words
["the", "a", "an", "and", "in", "of"]
end
end
Where does the argument go to first? The attr_accessor or the def title ... ?
attr_accessor :title defines two methods for you: title (the "reader") and title= (the "writer"). When you do def title after attr_accessor, you replace the title method entirely with your new method.
In other words, the method generated by attr_accessor is never called, because you've overwritten it with your own method.
Since you're writing your own "reader" method, you should just use attr_writer:
class Book
attr_writer :title
def title
capital_it(#title)
end
# ...
end
First of all, you have an issue in your def capital_it(title) method.
.map method will return a new array which you didn't assign to anything. If you want to reassign it to local variable word_arr use .map! instead.
You should probably rewrite condition word.capitalize! unless little_words.include?(word) to this little_words.include?(word) ? word : word.capitalize
P.S. In your case it works because you use capitalize! on each string of element but the style of writing like that isn't good.
So I'd recommend you to write your class like this one:
class Book
SKIP = %w(the a an and in of)
attr_writer :title
def initialize(title)
#title = title
end
def title
capital(#title)
end
private
def capital(title)
array = #title.split(/\s+/)
array.map! do |word|
SKIP.include?(word) ? word: word.capitalize
end
array.join(" ")
end
end

Trouble passing through rspec

I'm trying to pass this rspec
describe "title" do
it "should capitalize the first letter" do
#book.title = "inferno"
expect(#book.title).to eq("Inferno")
end
it "should capitalize every word" do
#book.title = "stuart little"
expect(#book.title).to eq("Stuart Little")
end
With this code
class Book
attr_accessor :title
def initialize(title="")
#title = capital(title)
end
def capital(title)
articles = %w(the a an and of in the)
new_title = []
title.split.each do |w|
articles.include?(w)? new_title << w : new_title << w.capitalize
end
new_title[0] = new_title[0].capitalize
new_title.join(" ")
end
end
book = Book.new("stuart little")
puts book.title
And I get "Stuart Little" when I run the code, but I keep getting errors when I run it through rspec. (eg, it just returns as "stuart little").
I'm thoroughly confused why this is happening, so I'm hoping someone can shed some light for me.
You only run your capital method, when you assign a title to the new method, but not when you assign it to the title= setter method.
I would replace the attr_accessor with a attr_reader and add a custom title= setter method:
class Book
attr_reader :title
def initialize(title = '')
self.title = title # calls the setter below
end
def title=(title)
#title = capital(title)
end
private
def capital(string)
articles = %w( a an and in of the )
words = title.split.map do |word|
articles.include?(word) ? word : w.capitalize
end
words.join(' ').capitalize
end
end

How to yield an instance method from another file for Rspec

The code below checks to see if in the file '08_book_titles' I have reversed the string that is in the class Book and method title contained in the code below. That string being "inferno".
require '08_book_titles'
describe Book do
before do
#book = Book.new
end
describe 'title' do
it 'should capitalize the first letter' do
#book.title = "inferno"
#book.title.should == "Inferno"
end
I tried the following to no avail. Any help is much appreciated.
class Book
def title
return yield.capitalize
end
end
If you want to store the capitalized form, then
class Book
attr_accessor :title
def title=( title )
#title = title.capitalize
end
end
If you want to preserve the original form

class Book
attr_accessor :title
def title
#title.capitalize
end
end

Ruby: Class methods and initialization

Currently struggling through an rspec tutorial and would really appreciate some clarification.
Code is:
class Book
attr_reader :title
def initialize(title=nil)
#title = title = title && title.capitalize!
end
def title=(new_title = nil)
#title = new_title && new_title.each do |word|
word.capitalize!
end
end
Two questions:
Why are there two sets of #title (that is: why is it defined in both initialize and title as being set = to different things)?
Why does the title method have an = after it? The code breaks if I do not use the =.
edit: for the purposes of my rspec tutorial this is the code i finally tried that worked
class Book
attr_accessor :title
def initialize(title = nil)
#title = title
end
def title=(book_title = nil)
#title = book_title.capitalize
end
end
My initial problem was with the title= method. Finally I came upon a thread that explain what method= function was. It is necessary if you want to assign a value to something within a class method (at least that is my understanding at this point. Feel free to correct me).
I would appreciate any tips in this new code as well.
Let's analize that:
attr_reader :title
Here we are basically defining the method:
def title; #title; end
which returns the instance variable #title.
def initialize(title=nil)
#title = title = title && title.capitalize!
end
Here we are defining a 0-1 arguments constructor which can be reduced to:
def initialize(title=nil)
title && #title = title.capitalize
end
The fact is that title within the constructor is the argument variable and not the title or title= method, therefore the title= method defined later is never called here. Notice that && is used for short-circuit evaluation here.
def title=(new_title = nil)
#title = new_title && new_title.each do |word|
word.capitalize!
end
Here we actually have two syntax errors: the first one is that for Strings (which I assume is the type of a title as it appears to call String#capitalize! later) does not have the each method. Whoever wrote this probably meant String#each_char or to String#split it first instead.
The second error is that the block after the each is not closed with an end.
Now assuming this version instead:
def title=(new_title = nil)
#title = new_title && new_title.split(' ').each { |word| word.capitalize! }.join(' ')
end
the title= would just assign title to the #title variable for the same reason (short-circuit evaluation) as before and could be reduced to:
def title=(new_title = nil)
new_title && #title = new_title
end
The initialize method is called when an instance of the class is constructed. The #title = ... there sets the initial value of #title.
The title= method is called when someone subsequently sets the value of title on an instance of the class. It then adjusts the value of #title accordingly. See Ruby Accessors for a detailed explanation.
As an example:
book = Book.new # calls initialize
book.title = 'foo' # calls title=

Need help to customize a class attribute in ruby?

I am trying to create a Book class, with 1 attribute: title, which must be capitalize if entering in lowercase. My code works in repl.it but rspec still show NoMethodError (undefined method 'title' for #(Book.... #title="Inferno")
My code:
class Book
def initialize(title=nil)
#title = title
end
def title=(new_title)
title = new_title.capitalize!
end
end
Rspec:
require 'book'
describe Book do
before do
#book = Book.new
end
describe 'title' do
it 'should capitalize the first letter' do
#book.title = "inferno"
#book.title.should == "Inferno"
end
Thank you.
try this out.
class Book
attr_reader :title
def initialize(title=nil)
#title = title && title.capitalize!
end
def title=(new_title)
#title = new_title && new_title.capitalize!
end
end
class Book
def initialize(title=nil)
#title = title
end
def title=(new_title)
title = new_title.capitalize!
puts title
puts #title
end
def title
#title
end
end
b = Book.new('hello')
b.title = 'hello'
--output:--
Hello
hello
#title and title are two different variables.

Resources