Hi, I'm Nicholas Johnson!

software engineer / trainer / AI enthusiast

Checking if an object is a subclass

Short tip today, checking if a class is a subclass of another object.

class Emotion; end
class Lassitude < Emotion; end

We can use is_a? to test if an instance is an instance of a class or not

feeling_this_morning = Lassitude.new
feeling_this_morning.is_a? Lassitude
# => true
feeling_this_morning.is_a? Emotion
# => true
feeling_this_morning.is_a? Object
# => true

If we want to test if the class itself is a subclass though, we can’t use is_a? because:

Lassitude.is_a? Class
# => true

Instead we use the less than < operator

Lassitude < Emotion
# => true

If we want to include the class itself we can use the <= operator

Lassitude <= Lassitude
# => true

Lassitude <= Emotion
# => true