Today I Learned

hashrocket A Hashrocket project

Comparing Class Hierarchy Relationships

The comparator methods (<,>, etc.) can be useful for a lot of things. In Ruby, they can be used to compare classes in order to understand how they relate to one another on the class hierarchy.

# Fixnum is a subclass of Integer
> Fixnum < Integer
=> true
# Integer is not a subclass of Fixnum
> Integer < Fixnum
=> false
# Fixnum and String are not related to one another
> Fixnum < String
=> nil

The < operator will tell you if there is a subclass relationship. The > operator will tell you if there is an ancestor relationship. When nil results, it means the two classes do not have a direct relationship.

There are a few more of these types of operators on the Module class.


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.