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.
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.
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
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.
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