CodeX
Published in

CodeX

Ruby Classes, Objects & Methods

Classes, objects, and methods are extremely important concepts in Ruby and even though you may (or may not) be using them already, in order to really understand how your code is working under-the-hood it is critical to grasp these foundational components of the Ruby language. In this post I will be covering what each of these are and how to create and use them.

Let’s Dive In

Ruby Classes

Ruby is an Object-Oriented language, meaning it relies on the concept of classes and objects to structure a program into simple and reusable pieces of code that act as blueprints to create individual instances of objects. Ruby classes are the blueprints. In creating a custom class, you are creating a new data type.

As a programmer you build these classes to represent real-world entities (ex. Book class representing all books), and the class is used as the set of rules (or blueprint) in creating an instance of that class (ex. a singular Book). The class defines what attributes an instance of the class will have (ex. a Book will have title, author, description, and rating attributes), but it does not determine what the values of those attributes will be, and the class determines what an instance of an object is able to do (ex. a Book can be added or deleted).

To create a new class you employ the following syntax:

class Book
end

In order to define what attributes all Books should have, you need to provide them within the class. We are not going to cover migrations and databases in this post, so in order to define the attributes for all Books we will use attr_accessor. Attr_accessor creates two methods (instead of manually having to write them out yourself in the class)- the getter method and the setter method. The getter method is used to retrieve the value of an attribute, while the setter method is used to assign a value to an attribute. Again, these are both created behind the scenes when you use attr_accessor:

class Book
attr_accessor :title, :author, :description, :rating
end

So, with our current iteration of the Book class, all instances of Book will need to have a title, author, description, and rating.

Ruby Objects

Objects of the Books class!

As mentioned above, classes (ex. the Book class) are used as the blueprint for creating instances of the class. An object is an individual instance of a class (ex. a singular Book). In the Ruby language, everything is an object (with the exception of conditionals, blocks, and messages)- so a class is also an object.

Taking the books example out of the parentheses- in object-oriented terms, the individual Book is an instance of the class of objects known as Book. In our example, a Book is comprised of a title, author, description, and rating- these are the attributes of the Book class and when an instance of the Book class is created the values (data) corresponding with these attributes will be assigned.

To create a new Book object, you use Ruby’s new method on the class and supply the values for the class’s defined attributes:

first_book = Book.new()
first_book.title = "Really Great Book"
first_book.author = "Denali Balser"
first_book.description "best book ever"
first_book.rating = 100

Above we created a new instance of the Book class, aka an object, and assigned it to a variable (first_book) and then used Ruby’s dot notation to assign values to the attributes of the instance. In order to access the attributes of this object you employ the following syntax:

puts first_book.title #=> Really Great Book

Ruby Methods

A Book may also have functions other than just creating an instance of itself. This is where methods come into play. Ruby methods bundle one or more repeatable statements into a single unit. Methods are defined on an object, aka within a class — a class can be comprised of a combination of characteristics (attributes) and functionalities (methods).

Following Ruby convention, method names should begin with a lowercase letter, if it were to begin with an uppercase letter it may be interpreted as a constant and the call may be parsed incorrectly. Additionally, Ruby methods must be defined before being called, otherwise an exception for undefined method invoking will be raised.

As an example, the Book class may have a method called change_author that changes the author attribute of a Book instance. This method would be defined within the Book class as it is specific to the class.

Syntax for creating a method:

def change_author 
#method body (code for changing the author)
end

Methods can also accept arguments; with our example method you may want to supply the name of the new author you would like to replace the existing one with. In order to do so you need to write the method with a parameter:

class Book 
attr_accessor :title, :author, :description, :rating
#define change_author method to accept arguments:
def change_author(obj, new_author)
obj.author = new_author
end
end#create new Book object and assign to first_book variable:
first_book = Book.new()
first_book.title = "Really Great Book"
first_book.author = "Denali Balser"
first_book.description "best book ever"
first_book.rating = 100
#call change_author method on first_book object with obj and new_author arguments:
first_book.change_author(first_book, "J. R. R. Tolkien")
#display the author of first_book object in console:
puts first_book.author #=> J. R. R. Tolkien

Now when you call the change_author method you will supply the object you want to alter (obj) and the new_author argument and that will be used in the body of the method to update the Book instance’s author attribute.

All Together Now

To recap, classes define what an instance - or individual object - of that class will look like (like a blueprint), an instance is an object, and methods (defined within the class) describe what an instance of the class/object will be able to do.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Denali Balser

Denali Balser

Full-stack software engineer experienced in Ruby on Rails, React, Redux, and JavaScript based programming.