Skip to content

Instantly share code, notes, and snippets.

@beforeyouknowit
Forked from donburks/boo.rb
Last active August 29, 2015 14:08
Show Gist options
  • Save beforeyouknowit/e9d7a46a4181bc347070 to your computer and use it in GitHub Desktop.
Save beforeyouknowit/e9d7a46a4181bc347070 to your computer and use it in GitHub Desktop.
require_relative 'monster'
require_relative 'vampire'
blargh = Monster.new("big", "blue", "short")
puts "BLARGH"
blargh.describe
puts "Hey Blargh, what are your horns like again?"
puts "My horns are #{blargh.horns}"
blegh = Vampire.new
blegh.size = "fat" #resetting for this ONE instance
puts "BLEGH"
blegh.describe
blegh.laugh(5)
blegh.colour = "red"
puts "Blegh's colour is #{blegh.colour}, damnit!"
blargh.size = "slightly smaller, since he lost weight"
puts "BLARGH"
blargh.describe
blargh.laugh(3)

Question 1

What is a Class vs an instance of a Class?

An object is data, and the methods with which to act on that data.

Class is a prototype of the methods which we will use on an instance of data.

And instance is a set of data, to which we are applying the methods of a class.

Question 2

What is the difference between regular (local) variables and @instance variables?

A local variable is only available to the method in which it was defined.

An instance variable is available to all methods within an instance of a class.

Question 3

What does attr_accessor do for us? What's an alternative to using attr_accessor which would achieve the same outcome?

attr_accessor allows you to read and write to variables from outside an instance of a class.

You could manually write your own getter and setter methods.

def value=(something)

end

Question 4

What is the purpose of the initialize method used in classes? Do you have to specify an initialize for one of your classes?

The initialize method is the one method that is guaranteed to run when a new instance of a class is created. It can accept parameters which you can use to assist in setting up your instance. It can also run any other associated code that is necessary to instantiate the object.

It is not required. But if it is there, it will be run when you create a new instance of an object.

Question 5

Assuming a class Computer exists in our program, write out the one-line code to create an instance of it and have a variable called computer pointing to that instance.

computer = Computer.new
class Monster
attr_accessor :size, :colour, :height
attr_reader :horns
def initialize(size, colour, height)
@size = size
@colour = colour
@height = height
@horns = "Looooong and spiky" #not passed in, but still an instance variable
end
def describe
puts "I am a #{size} #{self.class}." #used self here, because 'class' is also a keyword
puts "My colour is #{colour}, and I am #{height}"
puts "My horns are #{horns}" #I can do this here without the '@' because of the attr_reader
giggle
end
def laugh(count)
count.times do
giggle
end
end
private
def giggle
puts "Tee hee hee"
end
end
class Vampire < Monster
# ALL METHODS AND ATTR_* properties are inherited!
def initialize
#This method overrides the Monster initialize method
#Therefore, in order to get the functionality of that method,
#I am going to user super
#Super calls the method that you are trying to override.
#Or, more explicitly, super travels up the inheritance chain
#calls the first instance of the method of the same name as the
#one in which you called it. (Hence, initialize -> initialize)
super("skinny", "pale", "tall")
@fangs = "sharp"
@age = "immortal"
@horns = "nonexistent, I'm a Vampire!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment