Type-specific code with if statement or with case/when

The answers to [ Nillable attribute in Struct helped, but I still could not figure out why this does not work:

struct Dog
  getter name  : String
  getter owner : String
  def initialize (@name, @owner)
  end
end

struct Cat
  getter name  : String
  getter staff : String
  def initialize (@name, @staff)
  end
end

if Random.rand() < 0.5
  animal = Dog.new("Gin", "Foo")
else
  animal = Cat.new("Tonic", "Bar")
end


p! animal
p! animal.class
if animal.class == Cat
  p! animal.staff
else
  p! animal.owner
end

OTOH this does:

case animal
when Cat
  puts "cat"
  p! animal.staff
when Dog
  puts "dog"
  p! animal.owner
else
  puts "other"
end

== is a method that can be overwitten, so the types aren’t restricted. It only works with is_a?, or case (which uses is_a? under the hood)

Thanks. So this works:

if animal.is_a?(Cat)
  p! animal.staff
end