Zoonk icon

How to find a variable's type in Ruby?

Created by Will Ceolin
Created on Feb 17, 20 - Updated on Feb 17, 20

Sometimes it might be useful to know what type a variable has. Because variables are just objects, you can use the method to get its class (or type):

> name = "Da Vinci"
> name.class
=> String

> position = 42
-> position.class
=> Integer

You can also check if a variable has a certain type by using the method:

> name = "Isaac Newton"
> name.kind_of? String
=> true

> name?.kind_of? Integer
=> false