Object feels and introspection

One of the most interesting concepts I’ve learned so far about the Ruby programming language is the idea of introspection. In computing, type introspection is the idea that an object or program knows about it’s own properties at runtime.

Specifically in regards to Ruby this means that an object is self-aware and knows what it is, what values it contains and what’s going on around it at a given moment in time. In short, Ruby objects can have feels.

Similar to our good friend Keanu, a Ruby object can have a moment of reflection at a given moment in time. Let us make code:


x = "hello world"
x.class #=> String

x = :hello_world
x.class #=> Symbol

This is a relatively straightforward example that shows how Ruby is able to reflect on itself at two separate moments in life. We can ask Ruby what type of object it is, what type of methods we can call it and then infer more information about it’s scope given that information.

 

The importance of introspection

At this moment you’re probably asking yourself a number of questions.
What’s the big deal? Who cares if “Hello World”.is_a?(String) knows that it’s a string. It doesn’t seem all that complex or exciting.

WRONG.

Although by nature it’s a relatively simple concept, the building blocks that introspection provides come together to form the Voltron that is object orientation. The building blocks of introspection form complexities which allow us to create objects that can interact with each other.

Furthermore, introspection is crucial in our use of mixins, modules and class methods. At the most fundamental level introspection allows us to do all things Ruby and for that we must be forever grateful.


based_god = Class.new
young_based_god = Class.new(based_god)

lil_b = based_god.new
brandon = young_based_god.new

lil_b.instance_of?(based_god) #=> true
brandon.instance_of?(based_god) #=> false
brandon.kind_of?(based_god) #=> true

In our example we create two distinct classes, based_god and young_based_god with based_god being a superclass of young_based_god. We next create two instances of each of these two classes.

Ruby has a method called instanceof? that allows us to check the class of an instance. When comparing both instances we find that only lilb is an instance of the Class based_god.

However, when we use the Ruby method kindof? it is clear that Ruby knows that brandon is a subclass of *basedgod. Through introspection the language implicitly knows that *based_god is a superclass of young_based_god.

The idea of introspection is helpful to us as programmers because we do not have to guess the type of an object. If we want to know what an object is or what methods are available to us to use on that objects we can just ask.

Introspection means that we can make two different objects interact with each other in very unique ways and still be confident that their behavior will do what we expect. It is key to the beauty of Ruby and part of the reason it flows like a natural human language.

Thank you Matz and thank you based god.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s