Introduction
In this article, we are going to learn about how we can check whether an object is of the right type in order to interact with a particular class or module in Ruby. But before diving into that, let's first have some brief about the Ruby classes and module just to have a better understanding.
Ruby is a dynamic and purely object-oriented programming language that was developed by Yukihiro Matsumoto. It is a high-level programming language with much easier syntax so that it can be learned easily.
Now, what is a class in Ruby? A class is like a template for the program code that can be accessed through an instance object of the class. We can implement various methods in a class according to our requirements.
Classes in Ruby
In Ruby, we use the class keyword to initialise the class with the class name where the first letter of the class should be capitalised, and within the class, we can add some code and close with an end keyword.
Syntax
class Name_of_class
#statement
end
Example
class Alert
def display_message
puts 'welcome to coding ninjas'
end
end
#object creation
obj = Alert.new
#calling the method with the object
obj.display_message
Output
Module in Ruby
A module is a compilation of class variables, methods and constants. Unlike class we do not need objects to access the module data. All the classes are modules but not all modules are classes.
Syntax for module
It initialises with the module keyword with the name of the module and the first letter should be capitalised.
module Name_of_module
#statements
end
Example
module Display
# method in module should have the prefix of module name
def Display.message
puts 'here is the displayed message'
end
end
#calling the message method in module
Display.message
Output
Problem statement
Our main goal is to check whether the object is of the right type for the class or module methods. There are ways through which we can check that or we can say there are two typing techniques to do that.
Duck typing

Duck typing is a programming typing technique used in Ruby. As you know, in Ruby, there is no need to specify the datatype of the object like in many other programming languages. We can just declare a variable and assign a value to it. So in duck typing, we do not check the class. Instead, we check how that particular object will respond to an operator or to a method(object.respond_to? Operator or method_name). If that object supports the Operator or method, then that object is right for the purpose.
Example
class Ducktyping
#declaring the method
def objectchecking(x)
#checking object x response to upcase and to_i method.
puts 'object class is:',x.class
puts x.respond_to? :upcase
puts x.respond_to? :to_i
end
end
obj = Ducktyping.new
obj.objectchecking('hello')
obj.objectchecking(5)
obj.objectchecking(true)
Output
In the above example, we can observe how the different types of objects are responding to the method upcase and to_i. The upcase method converts the string into uppercase and to_i converts the number to an integer and string also. In the output string object is responding true for both the methods. This means that the string object is compatible with both methods. Second, an integer object is true only for to_i, not with an upcase method, and boolean is false for both.
Class typing
In class, what we do is we just type the logic of the program where we can check whether that particular object belongs to a particular data type or class or not (object.is_a?classtype).
Example
class Classtyping
#declaring the method
def objectchecking(x)
#checking the class of object x
if x.is_a?String
puts "welcome #{x}"
else
puts 'object x does not belong to string class'
end
end
end
obj = Classtyping.new
obj.objectchecking('Ninja')
obj.objectchecking(10)
obj.objectchecking(true)
Output
In the above example we only want to print an object on the console if it belongs to the string class. That's how the class typing works in programming. Where we are considerate about the object class.
Though it is generally advised to use duck typing in most of the cases because of the functionality it provides, it is also necessary to use class typing with duck typing so that the functionality of the code can become better.
Example
def append_objects(obj)
unless obj.respond_to? :<<
puts "cannot be appended"
end
if obj.is_a?Numeric
puts "cannot append a number"
else
puts obj << obj
end
end
append_objects('CodingNinjas')
append_objects(4)
Output
In the above example we can see that we can append a string with the << operator. We can also use a number with the << operator but it does not get appended as it is supposed to. It will return a multiplied value for a number like for 4 it will give us 64 so that we have to use Is_a to exclude the number objects in the program. That is how we can use both types of techniques in a program.