Skip to content

Instantly share code, notes, and snippets.

@PamBWillenz
Last active August 29, 2015 14:18
Show Gist options
  • Save PamBWillenz/e9d840ea6244b742970f to your computer and use it in GitHub Desktop.
Save PamBWillenz/e9d840ea6244b742970f to your computer and use it in GitHub Desktop.
INTRO TO CLASSES - Bloc Checkpoint
Getter and setter methods are so common that Ruby provides a shortcut to create them. The attr_accessor method creates a getter and setter method based on an argument. You call the attr_accessor with a Symbol argument:
class Square
attr_accessor :size
end
s = Square.new
s.size = 10 # This is the setter
s.size #=> 10 # This is the getter
Notice how we have both the getter and setter methods for size, because we called:
attr_accessor :size
What's going on here? Ruby is essentially writing the size=() and size methods for you when you add attr_accessor :size to your class.
To complete this exercise, create a class named Playlist. It should have attributes for name, author, and song_list. These attributes should readable and writable.
SPEC
describe "Playlist" do
describe "instance variables" do
it "should be able to create a country playlist" do
playlist = Playlist.new
# Setters
playlist.name = "Country"
playlist.author = "Blake Shelton"
playlist.song_list = ["Sure Be Cool If You Did", "God Gave Me You"]
# Getters
expect( playlist.name ).to eq("Country")
expect( playlist.author ).to eq("Blake Shelton")
expect( playlist.song_list ).to eq(["Sure Be Cool If You Did", "God Gave Me You"])
end
it "should be able to create a Rock playlist" do
playlist = Playlist.new
# Setters
playlist.name = "Rock"
playlist.author = "R&R"
playlist.song_list = ["Radioactive", "Clouds"]
# Getters
expect( playlist.name ).to eq("Rock")
expect( playlist.author ).to eq("R&R")
expect( playlist.song_list ).to eq(["Radioactive", "Clouds"])
end
end
end
CODE
class Playlist
attr_accessor :name
attr_accessor :author
attr_accessor :song_list
end
Create a Book class with two instance methods. The set_title_and_author instance method should act as a setter for two attributes by those names -- taking title and author arguments and setting them as attributes on a book instance. There is no direct test spec for this method, its implementation will be tested indirectly in the description method (below).
Note that this "setter" method doesn't have a = in its title. The equal sign is just a convention for clarity. It is unecessary here, because the method name itself implies its purpose.
The description method should return a string with both instance variables, formatted as:
"Title was written by author"
Remember that the words "title" and "author" will be replaced by instance variables defined in the title_and_author method.
SPEC
describe "Book" do
describe "description" do
it "should return title and author in description" do
book = Book.new
book.set_title_and_author("Ender's Game","Orson Scott Card")
expect( book.description ).to eq("Ender's Game was written by Orson Scott Card")
end
end
end
CODE
class Book
def set_title_and_author (title, author)
@title = title
@author = author
end
def description
"#{@title} was written by #{@author}"
end
end
As we learned in the checkpoint, getter and setter methods allow you to access attributes of a class instance.
Let's look at another example as a refresher. Below we define two methods in the Car class. The color=(c) method sets the @color instance variable, and the color method gets the @color instance variable. The last two lines of code below demonstrate how the getter and setter methods are used on the ferrari instance of the Car class:
class Car
def color=(c)
@color = c
end
def color
@color
end
end
ferrari = Car.new
ferrari.color= "Red" # setter
ferrari.color # getter
#=> "Red"
SPEC
describe "Book" do
# describe "instance variables" do
# it "should be able get and set the title to 'LOTR'" do
# b = Book.new
# # Ability to get/set title
# b.title = "LOTR"
# expect( b.title ).to eq("LOTR")
# # Ability to get/set pages
# b.pages = 1000
# expect( b.pages ).to eq(1000)
# # Ability to get/set author
# b.author = "J. R. R. Tolkien"
# expect( b.author ).to eq("J. R. R. Tolkien")
# end
# it "should be able to set the title to 'Hitchhiker's Guide'" do
# b = Book.new
# # Ability to get/set title
# b.title = "Hitchhiker's Guide"
# expect( b.title ).to eq("Hitchhiker's Guide")
# # Ability to get/set pages
# b.pages = 700
# expect( b.pages ).to eq(700)
# # Ability to get/set author
# b.author = "Douglas Adams"
# expect( b.author ).to eq("Douglas Adams")
# end
# end
# end
CODE
class Book
def title=(t)
@title = t
end
def author=(a)
@author = a
end
def pages=(p)
@pages = p
end
def title
@title
end
def author
@author
end
def pages
@pages
end
end
SPEC
describe MadeUpClass do
describe "#return_string" do
it "returns the right string" do
expect( MadeUpClass.new.return_string ).to eq("String to return")
end
end
end
CODE
class MadeUpClass
def return_string
p = "String to return"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment