Open In App

Introspection in Ruby

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Introspection is a method of metaprogramming in Ruby. It gives your Ruby code to analyse itself at run-time. It can be considered asking the program questions about its methods, instance variables, parents, ancestors and what functions it responds to. Introspection means to look inward. when our code asks questions about itself that is introspection in ruby.

Let us analyse this better with an example:
Example :




#creating a class and associated methods
class Geek
    attr_accessor :name, :stream
    @@organisation = "GeeksForGeeks"
    def initialize(name ="geek",stream="Computer Science")
        @name = name
        @stream = stream
    end
    def SayHi
        "Hello #{name}"
    end
    def Show
        "You belong to #{stream}"
    end
end


In the above snippet, we create a class called Geek with class as well as instance variables. We use default parameters for initialization. Now, let us describe certain important methods which help us in runtime Introspection and then we shall see it in practice.
Let us see the usage of these methods for Introspection in practice taking a few methods at a time:




# Creating an object and calling the methods
student1 = Geek.new()
puts student1.methods.inspect
puts student1.class.name
puts student1.respond_to?("SayHi")
puts student1.instance_variables
puts student1.class.class_variables


Output:

[:name, :name=, :stream, :SayHi, :Show, :stream=, :instance_of?, :kind_of?, :is_a?, :tap, :public_send, :remove_instance_variable, :instance_variable_set, :method, :public_method, :singleton_method, :extend, :define_singleton_method, :to_enum, :enum_for, :, :===, :=~, :!~, :eql?, :respond_to?, :freeze, :inspect, :object_id, :send, :display, :to_s, :nil?, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variable_get, :instance_variables, :instance_variable_defined?, :!, :==, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__]
Geek
true
@name
@stream
@@organisation

Explanation :
We first created an object called student1 and initialised it with the default parameters then on calling the methods we got the following outputs:

  1. .inspect : A method which returns all information about the object such as instance variables, class variables etc. in readable form. Here, we used methods.inspect to get a list of all the methods that the object responds to.
  2. .methods : returns all the methods that an object responds to. There are four types of methods generally : public, private, protected and singleton.
  3. .class : A method which returns the class whose instance an Object is The class here is Geek..
  4. .respond_to?(method name) : Returns a boolean value telling us whether the object responds to a certain method or not. Here, we see that the object responds to the method SayHi which is defined in class Geek.
  5. .class : A method which returns the class whose instance an Object is. We can see that the class variables are @name and @stream.
  6. .class_variables and .instance_variables : returns the class and instance variables defined in a class. We can see that the instance variable is @@organisation.

Example :




puts student1.class.superclass
puts student1.class.ancestors
puts student1.is_a?(Object)
puts student1.kind_of?(Object)
puts student1.instance_of?(Geek)


Output :

Object
Geek
Object
Kernel
BasicObject
true
true
true

Explanation:

  1. .superclass : A method which returns the super class of the class an object is an instance of. The method has to be chained with .class. All classes are subclass of Object
  2. .ancestors : A method which returns the ancestors of the class that an object is an instance of. The method has to be chained with .class. The basic ancestors get listed.
  3. .is_a? or kind_of? : These can be used almost interchangeably and returns true or false depending on whether an element belongs to the kind specified or not. We check if student1 is a kind of Object and the methods return true.
  4. .instance_of? : Returns true or false depending on whether an object is an instance of a class. We check if student1 is an instance of class Geek and the method returns true.

With the help of the above methods, run time introspection can be carried out and we can obtain useful information about the backstory of an object.



Last Updated : 04 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads