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
Tweet