
In this article I am going to talk about Object Oriented Programming with Ruby. After reading this article you understand:
- Why Object Oriented Programming?
- What are some Object Oriented Programming terminology?
- What Are Objects?
- How do Classes define Objects?
- What are modules?
- How does Ruby know where to look for methods?
I have been brushing up on my Ruby and Rails skills if you need to do the same please read my previous articles:
Ruby History/setup:
Ruby on Rails file structure:
Why Object Oriented Programming?
Often referred to as OOP, is a programming paradigm. As programs grow in complexity they become difficult to maintain. One small change would trigger a ripple effect of errors due to dependencies throughout the entire program.
OOP helps programmers create containers for data that can be changed and manipulated without affecting the entire program.
By creating container you can section off areas of code that perform certain procedures programs become the interaction of many small parts.
What are some Object Oriented Programming terminology?
Encapsulation: hiding pieces of functionality and making it unavailable to the rest of the code base, form of data protection, data cannot be manipulated or changed without obvious intention. It is what defines the boundaries in your application and allows you code to achieve new levels of complexity. Ruby, like many other OO languages, accomplishes this task by creating objects, and exposing interfaces(i.e methods) to interact with those objects.
Polymorphism: is the ability for data to be represented as many different types. “Poly” stands for “many” and “morph” stands for “forms”.
Inheritance: class inherits the behaviors of another class, referred to as the superclass. This gives Ruby programmers the power to define basic classes with large re-usability and smaller subclasses for more fine-grained, detailed behaviors.
Module: Is another way to apply polymorphic structure to Ruby programs. Modules are similar to classes in that they contain shared behavior. However, you cannot create an object with a module. A module must be mixed in with a class using the include method invocation. After mixing in a module, the behaviors declared in that module are available to the class and its objects.
What are objects?
“In Ruby, everything is an Object!”. Objects are created from classes. Think of classes as molds and objects as the things you produce out of those molds. Individual objects will contain different information from other objects, yet they are instances of the same class.
How do Classes define Objects?
Ruby defines the attributes and behaviors of its objects in classes. Think of classes as the basic outline of what an object should be made of and what it should be able to do. Classes start with class keyword followed by a Camel Case naming convention followed by the end reserve word to finish the definition. Ruby file names should be in snake_case, and reflect the class name.
# Person file name
person.rb# class name is person
class Person## Statement or code to be executedend # end of Person class# object of Person
employee = Person.new
In the above code I created an instance of Person class and stored it in a variable employee. Employee is an object or instance of class Person, the entire workflow of creating a new object or instance from a class is called instantiation. Basically the above code is saying that we instantiated an object called employee from the Person class. The important face is that obj object is returned by calling the class method new.
What are modules?
A module is a collection of behaviors that is usable in other classes via mixins. A module is “mixed in” to a class using the include method invocation. Lets say we wanted the Person class to have talk method but we have other classes that want to use the talk method as well.
module Talk
def talk(phrase)
puts sound
end
endclass Person
include Talk
endemployee = Person.new
employee.talk("Hello I am a person")
How does Ruby know where to look for methods?
When you call a method, how does Ruby know where to look for that method? Ruby has distinct lookup path that is followed each time a method is called. By accessing the ancestors, developers can find out the method lookup chain.
puts Peron.ancestors
----------- Person ancestors -------------------
Talk
I hoped you enjoyed learning about Ruby OOP and in the next article I will go into more detail about classes and objects.