Classes
All about classes in Ruby
Ruby is a Object Oriented Programming Language. The principles of the object-oriented programming language include:
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
Class 🗳️
- Describes in general what an
objectis with methods and variables. - For example:
Studentsis a classJose Valdivia, a particular student, is an object
- A class in ruby always start with
classkeyword followed by aname
class Students
variables
methods
endConstructor 📝
- Useful when creating an
objectwith specific values
class Student
def initialize(id, name, age)
@id = id
@name = name
@age = age
end
endObjects 📦
objectsare instances of theclass.- An instance means that objects (
child) are created based on their class (parent).
Instantiating
- We can create objects using the method
new - Similar to Java
student_1 = Student.new
student_2 = Student.newInstantiating with Constructor
- We can create objects with specific values
student_1 = Student.new("1", "Jose", "23")
student_2 = Student.new("2", "Kevin", "29")Access Modifiers 🔐
Local Variables
Local Variablesare the ones defined inside of a method scope. These are not available outside of scope.
Instance Variables
Instance Variablesare available across all objects but unique for each object.- In other words, an instance variable from one object differs from another object. For instance:
student_1.name = "Jose"
student_2.name = "Kevin"- Instance variables are preceded by the at sign
@followed by the variable name.
class Student
@id
@name
@age
endClass Variables
Class Variableare shared between all objects.- If the class variable from a object is modified, another object of the same class also updates it.
- Instance variables are preceded by the sign
@@followed by the variable name.
Attr_accesor vs Attr_writer vs Atrr_reader
- In ruby, methods are public, but data are private.
- Public: can be accessed outside of class
- Private: cannot be accessed outside of class
- For example
# Student has @id, @name, @age
student_1 = Student.new("01","Jose","23")
student_1.name # This gives error- To fix this, we do
gettersandsettersmethods. Such as
# Setters
def name
return @name
end
def name=(new_name)
@name = new_name- But it's tedious writing
gettersandsettersfor each instance variables, but there's another way
attr_reader
- Data are private
- We use this when we want to
- keep instance variables private from changing,
- but access them publicly (without
gettersmethods)
Class Students
attr_reader :id, :name, :age # <-- Getters
def initialize
code
end
end
# student_1.name => allowed
# student_1.name="Dave" => not allowedattr_writer
- Data are private
- We use this when we want to
- keep instance variables private from accessing them,
- but changing them publicly (without
settersmethods)
Class Students
attr_writer :id, :name, :age # <-- Getters
def initialize
code
end
end
# student_1.name => not allowed
# student_1.name="Dave" => allowedattr_accessor
- It's the combination of both attr_reader and attr_writer
Class Students
attr_accessor :id, :name, :age # <-- Getters
def initialize
code
end
end
# student_1.name => allowed
# student_1.name="Dave" => allowed