Introspection in Ruby Last Updated : 04 May, 2020 Summarize Comments Improve Suggest changes Share Like Article Like 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 : Ruby #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: Ruby # 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: .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. Example : Ruby 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: .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. Comment More infoAdvertise with us Next Article Introspection in Ruby T tirtharajsengupta Follow Improve Article Tags : Ruby Write From Home Ruby-Basics Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi 6 min read What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac 13 min read Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i 6 min read Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc 15+ min read Like