Ruby Classes
Objectives
Describe difference between objects and classes
Demonstrate how objects are referenced
Use getters and setters
Describe the uses of
attr_writer
,attr_reader
,attr_accessor
Define instance variables and instance methods
Define class variables and class methods
Utilize the
self
keywordExplain the difference between
Private
andPublic
methodsDescribe method chaining in a class
Class Definition of a person
Let's create our first class.
person.rb
This defines a class definition of a Person
. The class keyword denotes the begining of a class definition.
To create a new instance of our class we write the following:
A particular instance of a class is a called an object. In general, languages that use objects as a primary means of data abstraction are said to be Object Oriented Programming (OOP) languages.
Objects
What is an object in ruby? Basically everything that isn't a keyword.
However, this can cause you some headaches if you're not careful.
Imagine we had the following
Wow, the second array completely changed. That's because arr2
was a reference to arr1
. Both variables represented the same object. The way around this is to copy the object.
Initialize and instance variables
In our class definition we can make use of an initialize method, which is run when a new instance of the class is created.
We can also make use of instance variables that are defined for each particular object and are available throughout other methods in the object. These variables are prefixed by an @ symbol, i.e. @my_var
.
Now, when we create a new Person we are required to specify the name
of the person.
Getters and Setters
Having created an instance variable in our object, we might want to read that outside our object. However, we have to define a method that will act as an interface for reading for this variable called a Getter Method.
Now we can read or get the name outside the object.
Similarly, we may need to change or set an instance variable from outside the object, so we create a method called a setter method.
We can now get and set the name of a person using the methods we created for @name
.
Getters and Setters, the Ruby way
In ruby, the practice of creating a getter method is so common there is a shorthand that can be used at the top of a class definition called attr_reader
.
Similarly, we can do the same with the setter method using attr_writer
Moreover, we have a shorthand for telling our class to create both a getter and a setter method called attr_accessor.
Class and self
We just created instance variables, which have different values depending on the object instance. Class variables share the same value across the entire class. Also, we don't need to create an instance in order to access class variables.
Let's first create a variable associated with our class. Using the syntax @@var_name
designates a class variable.
We have to create a method on the class to make the class variable accessible. Now we can access the value without creating any people.
If we create a few people, we see the following
What about class methods? We can also create a method that belongs to a class.
In most cases, inside an instance method, self refers to the object, but when used in the context of a method name, self refers to the class itself`.
Also, note that self
can be used in instance methods to refer to particular object in use, i.e. self.var_name
instead of @var_name
.
Private Methods
If we create a class Person
with a name attribute and use attr_accessor
to create the getters and setters as follows
then anyone can read and access Person#name
.
We can change this using the private
keyword. Everything under the private keyword is private to outside users. Only the instance methods can use the getter and setter.
We can also add private methods by defining new methods below the private
keyword
Note that we can create a public method that calls a private method, because we are within the class.
Chainable methods
What if I wanted to create a class that had chainable methods calling many methods in one line.
Trying to do
to achieve this we have to return a reference to the object after each method
Last updated