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:
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:
[: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__]
We first created an object called student1 and initialised it with the default parameters then on calling the methods we got the following outputs:
- .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.
- .methods : returns all the methods that an object responds to. There are four types of methods generally : public, private, protected and singleton.
- .class : A method which returns the class whose instance an Object is The class here is Geek..
- .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.
- .class : A method which returns the class whose instance an Object is. We can see that the class variables are @name and @stream.
- .class_variables and .instance_variables : returns the class and instance variables defined in a class. We can see that the instance variable is @@organisation.
Object Geek Object Kernel BasicObject true true true
- .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
- .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.
- .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.
- .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.
- Ruby | Arrays
- Ruby | BigDecimal == value
- Ruby | Blocks
- Ruby | BigDecimal <= value
- Ruby | BigDecimal >= value
- Ruby | BigDecimal <=> value
- Ruby | Methods
- Comments in Ruby
- Ruby | BigDecimal === value
- Ruby | Operators
- Ruby Literals
- Ruby | Set <= method
- Ruby | Set > method
- Ruby | Hash == value
- Ruby Mixins
- Polymorphism in Ruby
- Ruby | Set >= method
- Ruby Directories
- Ruby | Module
- Ruby | BigDecimal % value
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.