- Read Tutorial
- Watch Guide Video
Ruby utilizes a unique syntax for creating setters and getters in a class. In this guide we will walk through how to implement these processes.
Creating a class is fairly simple in Ruby. It's so simple it wasn't even worth dedicating an entire guide to it (I build courses just like I code: I despise wasting my time or yours for dead simple concepts).
To define a class simple type the word class
followed by the name you want to give to your class, and end it with the word end
. Anything contained between class
and end
belongs to this class.
devCamp Note: Class names in Ruby have a very specific style requirement. They need to start with an uppercase letter and if they represent multiple words, each new word needs to also be a capital letter.
We're going to start by creating a class called ApiConnector
:
class ApiConnector end
Now classes in Ruby can store both data and methods. But how can we define what data should be included? In many traditional OOP languages such as Java you need to create two methods for each data element you want included in the class. One method, the setter
, sets the value in the class. The other methods, the getter
, allows you to retrieve the value.
The process of creating setter
and getter
methods for every data attribute can be tiresome and leads to incredibly long class definition. Thankfully Ruby have a set of tools called attribute accessors.
Let's implement some setters
and getters
for some new data elements for our class. Since it's an API connector it would make sense to have data elements such as title
, description
and url
. We can add these elements with the code:
class ApiConnector attr_accessor :title, :description, :url end
Let's now instantiate this class. We're going to have an entire guide dedicated to instantiation since it can be confusing if you've never used it before. At a high level what it means is:
I'm going to create an instance of that class, so that we can use it to do something.
When you merely create a class, it doesn't do anything by itself as it is simply a definition, however in order to work with the class we need to create an instance of it. This is just like how a blueprint to a house isn't actually a house, it simply defines what the house will look like. In order to live in the house you have to build it. Instantiating a class is like building a house based on a blueprint.
This class can be instantiated with this code:
api = ApiConnector.new
In this code we're creating a new
instance of the ApiConnector
class and storing it in a variable called api
.
Now that we have an object created we can use the variable api
to work with the class attributes. For example I can run the code:
api.url = "http://google.com/"
puts api.url
If you run this file it will result in the following output:
This code is using both the setter
and getter
methods:
api.url = "http://google.com/"
is setting theurl
attribute value to"http://google.com/"
api.url
is getting the value from the class
So, this is how you create a class and communicate with its data using attribute accessors.
In addition to creating attributes, you can also create methods in a class.
def test_method puts "testing class call" end
To access this method, we can use the same syntax that we utilized with the attribute accessors.
api.test_method
In the output, the message testing class call
will be printed.
So, this is how you work with getters
, setters
and methods
in a Ruby class.
The full class code is here:
class ApiConnector attr_accessor :title, :description, :url def test_method puts "testing class call" end end api = ApiConnector.new api.url = "http://google.com/" puts api.url api.test_method