Object Orientation in Ruby
A Brief Intro on the Basics of OO: Classes, Methods, & Instances
Ruby is an open-source, object-oriented programming language used to build web applications. Therefore, every value in Ruby is seen as an object. The benefit of this is that we can not only group data types together but also define functions that can be called on the bundle of data. This allows you to keep cleaner code without repeating the same lines of code for every similar piece of data.
Classes
In Ruby, a class is used to specify the format for creating bundles of data types. A class is a template for creating objects with similar characteristics and behaviors. For example, we can have a class name of Human, and in this class, we can have values for height, weight, hair/eye color, IQ, etc, and behaviors like breathing, walking, and shopping. To define a new class, use the class keyword followed by the name and close with the keyword end.
Instances of Class
Let’s say we want to add some humans to our class. To do this we are going to use the #new method on our class. This will create and save new instances or objects in our class. Each of these instances shares a class and therefore shares information and functions but remains unique.
Instance Methods
Instance methods can be called on any and all instances of the class. These methods add behaviors to the objects. Any instance methods defined inside the Human class can be used with any instance of Human (madonna, adele, or oprah). In the body of the class, you begin with the keyword def followed by the name of the method and closed with end.
Now all of our defined instances of Human (oprah, madonna, and adele) can use the #habitat method.
Instance methods have a local scope as they are defined within a class. If you tried to call #habitat on an instance of another class, the result will be undefined. Likewise, if you called #sings on any of our objects, it will throw an error work as it has not yet been defined in the Human class.
Get Rachel Heyvaert’s stories in your inbox
Join Medium for free to get updates from this writer.
If you want to see the methods available to an object, simply use #methods on the object. This will return in the console a large array of built-in Ruby methods available to your object.
Object-oriented programming also supports class methods that were not touched on in this post. Class methods are for the classes themselves, not objects. This topic can be explored further here.
Instance Variables
To give objects data initiated from a class you use instance variables. These variables are available in all instance methods of a class. However, instance variables are confined to an individual instance of a class. To continue with the Human class example, this is how we would provide unique data to differentiate our instances of Humans. Oprah is a billionaire and former talk show host. These values would not be appropriate for Madonna or Adele. You define an instance variable by prefacing the variable name with an @ symbol.
In the code above, value is the placeholder for the future argument that will be passed in. Then oprah is assigned the name value of “Oprah”. This was simpler and cleaner than creating individual local variables for each of our instances of class.
Initializing Objects
If you want to specify that your instances of a class are created with specific data, then you use the #initialize method. This method will automatically be called with#new.
For our Human class, let’s initialize our objects with an age.
Now the instances of this class have to be initialized with an age. When creating a new object, you need to pass the value in as an argument with the #new method. This can help save steps later and help to create more dynamic objects efficiently.
It is possible to add more data for our object initialization. A human is more than their age. After all, age is just a number. Let’s initialize our objects with instance variables corresponding to eye color, nationality, and sex. Take care to pass in the arguments in the order in which you want them to be assigned.
In the code above, oprah was created with values for age, nationality, and sex. When the initialization method was defined, sex was given a default value of ‘female’ since our other Humans are also female. If no value is provided for our sex value, then “Female” will be automatically assigned.
There is much more to explore and use in the world of Ruby and object-orientated Programming than can be covered in one blog post, but hopefully, this is a good place to start.
Happy coding!





