Back to Pixeltree
Tech
Jr. Jamboree: OOP in Ruby
Tiffanie
January 06, 2023
3 min

One of the first things that I was told when I first started my dev journey was that in ruby, “everything is an object”, and “objects can send messages to each other”, what does that mean? This is a humble blurb, a tiny toe-dip into exploring what these concepts mean in an object oriented programming language like ruby.

To start, I wanted to refresh what object oriented programming is in comparison to functional programming. They’re referred as different paradigms for programming. Functional programming is declarative, utilizing functions for the computer to do what you want and not necessarily caring about how it achieves the result. It focuses on functions, avoids sharing state and mutable data.

OOP is imperative, where you explain the steps of how to get exactly what you want, focusing on classes and objects. The main pillars of OOP are: abstraction, encapsulation, inheritance and polymorphism. The two main properties are states (attributes) and behaviours (methods).

All objects are instances of a class, and if everything in ruby is an object… To get an instance when you have a class you call .new on it. You can check if anything is an object through using the .class method. Something like a string is an object of the String class:

Objects operate in a system called The Object Model, regardless of if they are strings, numbers, classes, modules, etc. All classes have a link to a superclass, and there are three special classes: Object, Module, Class. To show what any class inherits from, we can use the .ancestors method:

The BasicObject class is the superclass to rule all classes. To show this visually, this image taken from skilldrick.co.uk shows the links in a comprehensive way:

You can also check an object’s id with the object_id method and methods method as they are available to all objects!

When I googled about objects sending messages to other objects for this post, I was surprised to find that I’d had already been doing this. Sending a message or calling method in ruby is done through dot notation. This can be seen from the previous examples like when an object gets called with .class, .method. or .ancestors! I think it should also be noted that while objects can receive any message, whether or not the method that corresponds to it is correct or exists is another thing.

  • everything in ruby is an object, all objects are instances of a class
    • when you have a class, you can call .new on it to get an instance
    • class is like a template, can check if anything is an object is through the class method
      • string is an object of the String class
    • two main properties: states (attributes) and behaviours (methods)
    • objects regardless of whether they are strings, numbers, classes, modules, etc., operate in a system called The Object Model
      • three special classes: Object, Module, Class
    • object_id method available to all objects
  • all classes have a link to a superclass
    • the ancestors method checks what any class inherits from
  • BasicObject class is the superclass to rule all classes
  • ~~Abstractionpolymorphisminheritance and encapsulation form four of the main pillars of OOP~~
  • methods that define their behavior are called instance methods
    • because they can be called on any instance of the class but not the class itself
  • class method, which can be called on the class itself and not on its instances. Class methods are named by prefixing the method name with self
  • ruby has a method called methods that show all the methods available to any object
  • Objects communicate with one another by sending messages. A message is a method call from a message-sending object to a message-receiving object. A message-sending object is a sender while a message-receiving object is a receiver.
  • I can send any message I like to our object with the . notation

Ref:


Tags

techOOPRubyJr develper
Previous Article
Why it’s Important to Fail in Order to Succeed

Tiffanie

Jr. Software Developer

Related Posts

Software development from across the pond - overseas application development
January 05, 2021
7 min
© 2023, All Rights Reserved.

Quick Links

AboutServicesContact Us

Social Media