Skip to content

Instantly share code, notes, and snippets.

@ryanholm
Last active August 29, 2015 14:02
Show Gist options
  • Save ryanholm/3ecdca4b5a7214df1bc4 to your computer and use it in GitHub Desktop.
Save ryanholm/3ecdca4b5a7214df1bc4 to your computer and use it in GitHub Desktop.
BLOC Intro to Classes
#CP1
class Book
def title_and_author(title, author)
# set title to instance variable
# set author to instance variable
@title = title
@author = author
end
def description
# return a string with both instance variables
# "Title was written by author
"#{@title} was written by #{@author}"
end
end
#instance methods && instance variables are the same thing?
#RSpec
describe "Book" do
describe "description" do
it "should return title and author in description" do
book = Book.new
book.title_and_author("Ender's Game","Orson Scott Card")
book.description.should eq("Ender's Game was written by Orson Scott Card")
end
end
end
#CP2
class Book
#settable
def title=(t)
@title = t
end
def pages=(p)
@pages = p
end
def author=(a)
@author = a
end
#gettable
def title
@title
end
def pages
@pages
end
def author
@author
end
end
#RSPEC
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"
b.title.should eq("LOTR")
# Ability to get/set pages
b.pages = 1000
b.pages.should eq(1000)
# Ability to get/set author
b.author = "J. R. R. Tolkien"
b.author.should 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"
b.title.should eq("Hitchhiker's Guide")
# Ability to get/set pages
b.pages = 700
b.pages.should eq(700)
# Ability to get/set author
b.author = "Douglas Adams"
b.author.should eq("Douglas Adams")
end
end
end
#CP2
class Playlist
attr_accessor :name
attr_accessor :author
attr_accessor :song_list
end
#RSpec
describe "Playlist" do
describe "instance variables" do
it "should be able to create a country playlist" do
p = Playlist.new
# Setters
p.name = "Country"
p.author = "Blake Shelton"
p.song_list = ["Sure Be Cool If You Did", "God Gave Me You"]
# Getters
p.name.should eq("Country")
p.author.should eq("Blake Shelton")
p.song_list.should eq(["Sure Be Cool If You Did", "God Gave Me You"])
end
it "should be able to create a Rock playlist" do
p = Playlist.new
# Setters
p.name = "Rock"
p.author = "R&R"
p.song_list = ["Radioactive", "Clouds"]
# Getters
p.name.should eq("Rock")
p.author.should eq("R&R")
p.song_list.should eq(["Radioactive", "Clouds"])
end
end
end
@ryanholm
Copy link
Author

instance methods && instance variables are the same thing?

@ryanholm
Copy link
Author

class Foo
def bar(a,b,c)
@A= a
@b=b
@c=c
end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment