Ruby - Modify Struct attribute immediately after it's being initialized - ruby

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

Related

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

Ruby method using class and instances

I am trying to write a method that returns "Title was written by author", where title and author are variables. I think I'm close, but I'm not sure what to add at this point.
class Book
def set_title_and_author=(title, author)
#title = title
#author = author
end
def set_title_and_author
"#{#title} was written by #{#author}"
end
end
Your problem is that methods ending in = are unusual in that they must have a single argument, and you are passing two (title and author). (Also, Ruby allows you to insert spaces before =; e.g., set_title_and_author = arg.)
I can suggest four ways you might fix that:
make the argument the array [title, author] (i.e., a single value);
change the name of the method to set_title_and_author (and rename the getter);
replace the method set_title_and_author= with two setters, title= and author=, which you can write out explicitly or have Ruby do it for you by executing the method attr_writer :title, :author or attr_accessor :title, :author; or
use the initialize method as the setter.
First way
class Book
def title_and_author=(arr)
#title, #author = arr
end
def title_and_author
"#{#title} was written by #{#author}"
end
end
b = Book.new
#=> #<Book:0x007f91e414eb78>
b.title_and_author=(["Moby Dick", "Herman Melville"])
#=> ["Moby Dick", "Herman Melville"]
b.title_and_author
#=> "Moby Dick was written by Herman Melville"
Notice that I renamed your methods by dropping set_. set is not needed in the first case and is misleading in the second, where get would be more appropriate, but again, is not needed.
Second way
class Book
def set_title_and_author(title, author)
#title = title
#author = author
end
def title_and_author
"#{#title} was written by #{#author}"
end
end
b = Book.new
b.set_title_and_author("Moby Dick", "Herman Melville")
b.title_and_author
#=> "Moby Dick was written by Herman Melville"
Notice that I've changed the name of the first method back to set_title_and_author. It can't have the same name as the getter method and, without the = on the end, the name needs modification to suggest what it does.
I prefer this to the "second way" (making the argument an array).
Third way
class Book
attr_writer :title, :author
def title_and_author
"#{#title} was written by #{#author}"
end
end
b = Book.new
b.title = "Moby Dick"
b.author = "Herman Melville"
b.title_and_author
#=> "Moby Dick was written by Herman Melville"
Fourth way
class Book
def initialize(title, author)
#title = title
#author = author
end
def title_and_author
"#{#title} was written by #{#author}"
end
end
b = Book.new("Moby Dick", "Herman Melville")
b.title_and_author
#=> "Moby Dick was written by Herman Melville"
This is a very common way of setting instance variables.
Following up on #orde's answer, I would also use the wonderful #attr_accessor method to automatically create setters and getters for title and author.
class Book
# create the setter and getter methods
attr_accessor :title, :author
# create an optional initializer
def initialize(title = nil, author = nil)
#title = title
#author = author
end
def print_title_and_author
puts "#{#title} was written by #{#author}"
end
end
book = Book.new("Blood Meridian", "Cormac McCarthy")
book.print_title_and_author
# => Blood Meridian was written by Cormac McCarthy
book.title = "Blue moon"
book.print_title_and_author
# => Blue moon was written by Cormac McCarthy
Creating different setters and getters for different attributes is preferred.
You can also create chain-setters, which is becoming a trend now, so you can set many attributes with one line of code. you do this by returning self after performing the task (setting the attribute):
# Reopen the class to add methods,
# the old code is still being used!
class Book
def set_title title
#title = title
self
end
def set_author author
#author = author
self
end
def to_s
"#{#title} was written by #{#author}"
end
end
#chain set attributes
book.set_title('My New Title').set_author('Me')
#print the output - notice the #to_s is implied
puts book
# => My New Title was written by Me
You don't need the initializer, but please notice that the current #print_title_and_author and #to_s methods produce a funny output if the attributes aren't set.
We can change that with an if statement (actually, I will use unless). Le't open the class again and re-define these methods:
# Reopen the class to add methods,
# the old code is still being used!
class Book
def to_s
return "author or title missing!" unless #author && #title
"#{#title} was written by #{#author}"
end
def print_title_and_author
puts self.to_s #DRY code - do not repeat yourself.
end
end
new_book = Book.new
puts new_book
# => author or title missing!
I think this is better.
Good Luck, and welcome to Ruby!
Instead of using a setter method to assign the title and author, you should use the initialize method to set up an object's initial state:
class Book
def initialize(title, author)
#title = title
#author = author
end
def print_title_and_author
puts "#{#title} was written by #{#author}"
end
end
book = Book.new("Blood Meridian", "Cormac McCarthy")
book.print_title_and_author
#=> Blood Meridian was written by Cormac McCarthy

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.

Printing variables

My problem is probably quite easy, but I couldn't find an answer anywhere.
When create a class, for example:
class Book
#author = "blabla"
#title = "blabla"
#number_of_pages"
I want to create a method to print out my variables. And here I'm getting a problem when I try:
def Print
puts #author, #title, #number_of_pages
end
I am getting nothing.
When I try:
def Print
puts "#author, #title, #number_of_pages"
end
I get straight: "#author, #title, #number_of_pages"
How can I make the Print method print out the variables' values?
You should move your variable initializations to initialize:
class Book
def initialize
#author = "blabla"
#title = "blabla"
#number_of_pages = 42 # You had a typo here...
end
end
The way you have it in your question, the variables are class instance variables (which you can Google if you're curious about, but it's not really relevant here).
Initialized as (normal) instance variables, your first version of Print() works if you're just looking to dump the state -- it prints each parameter on its own line.
To make your second version of Print() work, you need to wrap your variables in #{} to get them interpolated:
def print # It's better not to capitalize your method names
puts "#{#author}, #{#title}, #{#number_of_pages}"
end
In addition to the allready exellent answer of Darshan, here is the way you would do it optimally
class Book
attr_accessor :author, :title, :number_of_pages
#so that you can easily read and change the values afterward
def initialize author, title, number_of_pages = nil
#so that you don't really need to provide the number of pages
#author = author
#title = title
#number_of_pages = number_of_pages
end
def print
puts "#{#author}, #{#title}, #{#number_of_pages}"
end
end
my_book = Book.new("blabla", "blabla", 42)
my_book.title = "this is a better title"
my_book.print
#=>blabla, this is a better title, 42
I think Darshan Computing has already solved your problem very well. But here I would like to give you alternative ways of achieving that.
I assume that you'd like to print out all the instance variables you have in the class. The method instance_variables could return an array of all your instance_variables in symbols. And then you can iterate them do whatever you want. Please be careful: instance_variable_get is pretty convenient but not the best practice.
class Book
attr_reader :author, :title, :number_of_pages
def initialize(author, title, number_of_pages)
#author = author
#title = title
#number_of_pages = number_of_pages
end
def print_iv(&block)
self.instance_variables.each do |iv|
name = iv
value = send(iv.to_s.gsub(/^#/, ''))
# value = instance_variable_get(iv) # Not recommended, because instance_variable_get is really powerful, which doesn't actually need attr_reader
block.call(name, value) if block_given?
end
end
end
rb = Book.new("Dave Thomas", "Programming Ruby - The Pragmatic Programmers' Guide", 864)
# rb.instance_variables #=> [:#author, :#title, :#number_of_pages]
rb.print_iv do |name, value|
puts "#{name} = #{value}"
end
#=> #author = Dave Thomas
#=> #title = Programming Ruby - The Pragmatic Programmers' Guide
#=> #number_of_pages = 864
# You can also try instance_eval to run block in object context (current class set to that object)
# rb.instance_eval do
# puts author
# puts title
# puts number_of_pages
# end

Resources