Today I Learned

hashrocket A Hashrocket project

Check Subclass Relationships in Ruby

Today I learned the < operator can be used to compare classes and their relationships in their ancestry.

Let's say I have the following class setup:

class A end

class B < A
end

class C end

Then I can use < to determine if A is a subclass or ancestor of B, and also see that B is not related to C:

pry(main)> B < A
# => true

pry(main)> A < B
# => false

pry(main)> B < C
# => nil # no relation

There's also > which checks the inverse:

pry(main)> B > A
# => false

pry(main)> A > B
# => true

pry(main)> B > C
# => nil # still no relation

Docs

See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.