Rails Reflects for you A Brief on use of Reflection in Ruby on Rails codebase
<p>Reflection is the ability of a program to introspect itself, providing insight into its own structure and behavior at runtime. It enables inquiring about classes, methods, and attributes and also supports dynamic code manipulation.</p>
<p>There are methods in Ruby classes and objects that allow inspection of specific areas of code. In Ruby on Rails, reflection is primarily achieved through methods provided by the ActiveRecord::Reflection module.</p>
<p>Below are some of the methods provided in Ruby to explore more about the objects in runtime.</p>
<pre>
class Story
end
obj = Story.new
obj.class # Story
obj.is_a? Story # true</pre>
<p><code>class</code> method tells you the class of any object in Ruby. <code>is_a?</code> method lets us know if the given object is the instance of the class in the argument. It returns true for any subclass of the Class passed. If you want to check the direct instance alone, use <code>instance_of?</code>.</p>
<p><code>methods</code> return an array of symbols representing the available methods for an object or class and <code>instance_variables</code> returns an array of instance variable names for an object. Moreover, with Ruby’s metaprogramming, you can create methods on the fly with <code>define_method</code>.</p>
<p>Among the arsenal of reflective tools, the <code>source_location</code> method shines as a spotlight. It is part of the <code>Method</code> class and reveals the file path and line number where a method is defined.</p>
<p><a href="https://levelup.gitconnected.com/rails-reflects-for-you-84dc507d4dee">Click Here :-</a></p>