Learning Ruby Series 3 of 5

Nagaraj Bettadapura
3 min readApr 23, 2021

--

Ruby Class

A class is generally a collection of methods, and potential module(s). We learnt in the 2nd chapter of this series that multiple methods can be associated with an object. We also did a quick inspect of the hierarchy of the Ruby Object. The next evolution of this hierarchy is the Ruby class. Let us get back to experimenting in irb:

# We start off defining a class which is pretty simple
?> class C
?> end
=> nil
>> C.ancestors #Let us inspect the ancestors of the class C
=> [C, Object, Kernel, BasicObject] #C-->Object

As we have discovered, variables are some of the most common features of a program. A class is no different. There are local, instance and occasionally class variables used. Mostly it is local and instance variables. Accessing these variables in a class can happen through get and set methods defined from first principles. Example:

# Create a class User with 2 methods to set and get name
# @name is an instance variable. we use an instance variable so
# that it is not local to the set_name method, and can be accessed
# from outside the method
# We also use interpolation here #{@name} to access and display the # @name variable
?> class User
?> def set_name(arg_name)
?> @name = arg_name
?> puts "Name from instance variable @name #{@name}"
?> end
?> def get_name
?> puts "User Name being returned is #{@name}"
?> @name
?> end
?> end
=> :get_name

Now that we have defined the class, and the methods to get and set names, let us continue with our experimentation

# Let us create an instance of the User class and assign it to u 
# Instance of a class is one of the differentiator from an object
?> u = User.new
=> #<User:0x000055875adb7ab8> #returns the User instance
# User methods can be accessed by the "u" class instance
?> u.set_name("John")
John
Name from instance variable @name John
=> nil
>> u.get_name
User Name being returned is John
=> "John"
>> returned_name = u.get_name #Instance variable is accessed here
User Name being returned is John
=> "John"
>> returned_name
=> "John"

Let us explore the newly created instance “u” further

>> u.ancestors
NoMethodError (undefined method `ancestors' for #<User:0x000055875adb7ab8 @name="John">)
>> u.instance_of?(User)
=> true #f course u is an instance of User
# Since u is an instance, we now have access to the instance methods # of the User. We can inspect the instance_methods as below.Without # the "false" argument, we get to see default methods also. You can # try it out
>> User.instance_methods(false)
=> [:set_name, :get_name]

Luckily for us, ruby provides attr_* methods. This provides a cleaner and simpler way to code for access to variables.

attr_reader :name #replaces the User instance method :get_name
attr_writer :name #replaces the User instance method :set_name(name)

# Further simplifying attr_* function is the accessor that can
# replace attr_reader and attr_writer
attr_accessor :name

Class Inheritance

Inheritance is a popular OOPs concept that involves transferring hierarchically the parent’s behavior to the inheriting child, with the child having the ability to override, retain, and / or add-on additional characteristics

?> class User
?> def star
?> "Sun"
?> end
?> end
=> :star
?> class Earthling < User
>> end
=> nil
?> class Martian < User
=> nil
=> #<Earthling:0x000055875ac42c00>
>> puts desi.star + " is my Star"
Sun is my Star
=> nil
# Earthling and Martian classes now have access to the instance method defined in the User class since they are its instances.

--

--

No responses yet