Fundamental Concepts
- Decrease repetition
- Data and method encapsulation and hiding
- Atomic classes and functions
- Decouple classes and methods, the goal is to decrease module coupling and increasing cohesion.
The S.O.L.I.D principle
S → Separation of Concerns O → Open for extension, closed for modification L → Liskov Substitution principle I → Implement interfaces D → Decouple modules and classes
These fundamental principles of OOP extend to all languages, as they’re reflective of the paradigm and not a specific language. See my notes on OOP in this Java course, as it translates over.
Inheritance
To inherit from a class, use the syntax SubClass < SuperClass
the <
denotes inheritance.
Override methods
To override a method from the super class simply reimplement that class (exact same name) in the subclass.
Modules (Not OOP)
Modules can be used to bundle together functions that thematically fit together but need to be used around the project without instantiating a class instance.
Module Utils
def insert_bob() do
puts "Bob is here!"
end
def kick_jim() do
puts "Jim was kicked!"
end
end
To use functions in a module follow the syntax ModuleName.function()
Importing Modules into other Ruby files
Using the require
keyword, we can import a module and use all its functions.
The require
keyword requires a file path. But there’s also require_relative
that takes a relative path from the file that’s importing.