Object-oriented programming Flashcards
Ruby Class
class is used to organize and model objects with similar attributes and methods. Strings and integers are classes but with class we can create new custom classes. Always capitalized and use CamelCase
i.e.
class NewClass
# code for this class
end
initialize
Every time Person.new is called, the initialize method of the Person class is called.
method used to generate new instances of the class.
i.e. class Person def initialize # this code runs when a new instance is created end end
instance variables
the @ symbol is used to signify an instance variable. Instance variables hold a value specific to each instance of that class, not to all members of the class itself
i.e. class Student def initialize(name, grade) @name = name @grade = grade end end
In this example, name and grade are the instance variables.
.new
a method which creates a new class instance. Arguments to the class’ inititalize method can be passed in the .new call
i.e. class Fighter def initialize(name, style, division, age) @name = name @style = style @division = division @age = age end end
conor = Fighter.new(“Conor”, “mixed martial arts”, “Welterweight”, 31)
Class variables
class variables are attached to the class in which they are declared.
declared with @@ in front
Making a variable global from inside a class or method
insert $ in front of the variable
Inheritance
the process by which one class take on the attributes and methods of another. *a subclass can only derive from one parent class
syntax:
<
i.e. class ApplicationError def display_error puts "Error! Error!" end end
class SuperBadError < ApplicationError end
overriding methods
in an inherited class, you can define the same method from the base class to override that method.
i.e. class Creature def initialize(name) @name = name end
def fight
return “Punch to the chops!”
end
end
Add your code below!
class Dragon < Creature def fight #overrides fight from Creature class return "Breathes fire!" end end
super
to directly access the attributes or methods of a parent class / super class in a method you have overwritten
i.e.
lass Creature
def initialize(name)
@name = name
end
def fight
return “Punch to the chops!”
end
end
Add your code below!
class Dragon < Creature def fight puts "Instead of breathing fire..." super() end end
returns
Instead of breathing fire…
Punch to the chops!
public method
allow for an interface with the rest of the program. Can be called from outside the class
private method
can only be caled from within a class
attr_reader
attr_writer
methods used to read and write variables.
i.e. class Person attr_reader :name attr_writer :job def initialize(name, job) @name = name @job = job end end
att_accessor
method to both read and read a variable
Module
a module contains a set of methods, constants, or classes which can be accessed. Similar to a class but cannot create instances or have subclasses. written in CamelCase
i.e.
module Circle
PI = 3.141592653589793
def Circle.area(radius)
PI * radius**2
end
def Circle.circumference(radius)
2 * PI * radius
end
end
constants
variables that always stay the same value.
Written in all caps
i.e.
PI = 3.141