Skip to content

Instantly share code, notes, and snippets.

@mikedao
Created May 24, 2015 20:50
Show Gist options
  • Save mikedao/e9fe00530c837f746077 to your computer and use it in GitHub Desktop.
Save mikedao/e9fe00530c837f746077 to your computer and use it in GitHub Desktop.
OOP Notes
Create a Car Class conceptually
Class - Lay out the form for a category or type of an object.
Instance - A single realization or actualization of a class.
Google the characters in Hamlet, and you would get a list of Characters and Plays.
You get a list of significant characters, and not all characters.
If you were going to implement Hamlet in code, you would have to have a Horacio class.
class Horacio
end
h = Horacio.new
Whatever it means to be Horacio, you would define in the class, and then create an instance in the class. This is the idea of Horacio-ness and you would create one Horacio from that mold.
Contrast that with something like guard.
class Guard
new
g1 = Guard.new
g2 = Guard.new
g3 = Guard.new
guards = [g1, g2 g3]
They are all together so let’s make a guards array.
When items are unique and important we will have one class that gets instantiated one time.
Other times we are going to create forms, and create multiple instances of them. They are not the same, but they have some uniqueness, but they more or less have the same form.
Methods
The actions* that an instance can perform
one category of action is including questions, that can be asked and answered
These are generally verb-like.
Attributes
The properties (unique?) of a single instance
If you take a guard, some of the methods they would have are stand, stab, lay_down, retreat.
Attributes: weight, height, energy level, age.
Getter methods give you access to attributes.
age
Setter methods allow you to modify attributes.
age=
Side Note: Objects are the nouns.
Speech Code
Noun Classes and instances
Verbs Methods
adjectives Attributes
guard = Guard.new
guard is a local variable.
= is assignment operator
Guard.new happens first, results in an object, and is assigned to guard.
Variable Types:
Local - Exist only in the current scope
Instance - Exist only in the current instance
Class - Shared by all instances of a class
Global - Shared across the entire world
In terms of usage,
Global variables = 0.0% - $name
Class variables = 0.1% - @@name
Instance Variables = 4% - @name
Local Variables = 96% - name
Instance variables are attributes.
Scope
if you have a method
class Cohort
def max_age
max = 0
students.each do |s|
if s.age > max
max = s.age
end
end
end
end
max is a local variable. it is created on line 2 It dies when the method completes.
s is a local variable. it gets created on line 3, It only lives until the end of the enumerable at line 7.
If you change @max to an instance variable, it exists everywhere. This is a problem.
There are two ways instance variables typically get created.
Inside the initialize method.
To use the method attr_reader and give it a parameter with the symbol name. It returns the instance variable, but it doesn’t create it. attr_accessor will create it. You would get a method name and a method name= in this case.
Frames of Execution - Call Stack
script.rb
c = Cohort.new
c.students = [s1,s2,s3]
c.max_age
Line three calls a method. To get to line four, line three needs to complete.
A return value is the result of a completed method call. (Creating a frame and executing it) Default is the value of the last instruction or expression executed in a method and the override to have non default behavior is to use the return keyword.
Create a ruby program that is the model of a fish tank.
tank = FishTank.new
f1 = Fish.new(“Steve”)
f2 = Fish.new(“Daisha”)
tank.add (f1)
tank.add (f2)
tank.names
tank.counts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment