What is a class in Ruby?
Classes are the basic building blocks in Object-Oriented Programming (OOP) & they help you define a blueprint for creating objects.
Objects are the products of the class.
So what is an object?
An object is an individual “thing”, with its own identity & its own data.
For example:
A Book
class could be the blueprint for creating books.
This class defines what attributes ALL books have in common, like:
- a title
- an author
- pages
- etc.
Every individual book is an object & you can make many objects like it thanks to the Book
class. That’s the point of creating classes, they are reusable blueprints for your Ruby application.
Now:
You’re going to learn how to write your own classes so you can start writing OOP code, today.
Ruby Class Definition
The syntax for creating a class goes like this:
class Orange end
Notice that:
- Class names start with an uppercase letter.
- We use the
class
keyword, then theend
keyword. - An empty class is not very useful, but you can still create objects from it.
There are other ways to create a class (like Class.new
), but these methods are only useful in special situations.
Creating an Instance of a Class
The main use of a class is to be a container of methods, instance variables & constants, forming a blueprint from which you can create objects.
You can create objects using the new
method.
Like this:
Orange.new
The process of creating an object is known as “instantiation”, so we say that an object is an “instance” of a class.
Why create objects?
Because every object you create is different & unique.
Every object has its own identity.
For example:
With the Orange
class example, every orange
object you create has its own weight, country of origin, quality, etc.
And of course, it doesn’t have to be all about your favorite fruit, this works for a User class or any other class you want to define.
How to Make Ruby Classes More Useful
Classes become more useful when you start adding instance methods & instance variables to them.
A method is a thing your class can do.
For example:
You can squeeze an orange to get juice.
Here’s a code example:
class Orange def squeeze puts "Here's your juice!" end end orange = Orange.new orange.squeeze
These methods become commands for your objects!
Every Orange
object you create will have access to this squeeze
method & that’s one of the benefits of using classes.
An instance variable is something your class knows.
Example:
class Orange def initialize @juice_available = 100 end def squeeze @juice_available -= 50 end end
Instance variables are different from local variables because they start with the @
symbol. You can’t access them outside the class unless you define an attr_accessor.
What Class is It?
When working with objects in Ruby it’s helpful to know what class an object is made from.
You can do that like this:
"".class # String [].class # Array orange.class # Orange (assuming orange = Orange.new)
Why is this useful?
Methods are the little engines that make things happen in Ruby.
If you know the class you can find out what methods are available (use Google, ri
, or pry
), in other words, you can discover what the object can do for you!
Learning More
This is only the “tip of the iceberg” when it comes to classes.
If you want to learn more…
Read these:
- Ruby initialize method
- Attr_accessor, attr_reader, attr_writer
- Inheritance in OOP
- Object-Oriented Programming
Btw classes themselves are objects too, at least in Ruby 🙂
Summary
You have learned about classes in Ruby, how to create them & why they’re useful!
Don’t forget to share this article so more people can enjoy it.
Thanks for reading.