Trouble passing through rspec - ruby

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

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

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 - Modify Struct attribute immediately after it's being initialized

Suppose I have:
Book = Struct.new(:title, :content)
book = Book.new('harry potter', 'a bunch of content here')
p book.title #=> harry potter
What I want the last line to produce is "Harry Potter". I know I can do something like:
Book = Struct.new(:title, :content) do
def capitalized_title
self.title.gsub(/\S+/, &:capitalize)
end
end
and then call capitalized_title, but what I want is to not have to create a separate method, instead, have some way of when you assign "title" to a new Book object, the title is immediately capitalized. Some kind of hook method, I would guess.
class Book < Struct.new(:title, :content)
def title
super.gsub(/\S+/, &:capitalize)
end
end
book = Book.new('harry potter', 'a bunch of content here')
book.title # => "Harry Potter"
Book = Struct.new(:title, :content) do
alias orig_title title
def title
orig_title.gsub(/\S+/, &:capitalize)
end
end
To prevent title is called every time, override title=:
Book = Struct.new(:title, :content) do
alias orig_title= title=
def initialize(*args)
super
self.title = title
end
def title= value
self.orig_title = value.gsub(/\S+/, &:capitalize)
end
end
You could create an initialize method for the class Book (borrowing #falsetru's nice way of capitalizing the the words in title). First note that Book does not have its own initialize method;;
Book = Struct.new(:title, :content)
#=> Book
Book.private_instance_methods(false)
#=> []
Rather, it inherits from Struct:
Book.private_instance_methods.include?(:initialize)
#=> true
Book.instance_method(:initialize).owner
#=> Struct
Struct.private_instance_methods(false)
#=> [:initialize, :initialize_copy]
We add an initialize method in the usual way:
Book = Struct.new(:title, :content) do
def initialize(title,content)
self.title = title.gsub(/\S+/, &:capitalize)
self.content = content
end
end
confirm:
Book.private_instance_methods(false)
#=> [:initialize]
and test:
book = Book.new('harry potter', 'a bunch of content here')
book.title
#=> "Harry Potter"
book.content
#=> "a bunch of content here"
If there were more than just two instance variables, we might do this:
Book = Struct.new(:title, :content) do
def initialize(title,content)
super
self.title = title.gsub(/\S+/, &:capitalize)
end
end

Issue properly formatting ruby to pass rspec

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

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