How to Check a Value's Type in Ruby with is_a?
In Ruby, value.is_a?(Integer) returns true when value is an Integer, and false when it's anything else, such as a string that only looks like a number. It also returns true for a parent class, so 42.is_a?(Numeric) is true too.
Syntax
<value>.is_a?(<class>)
| Part | What it does |
|---|---|
<value> | The value to check. It can be anything, nil included. |
<class> | A class such as Integer, String, Array or Hash, or a module. |
| The result | true if <value> belongs to <class> or to a class below it, otherwise false. |
The Ruby documentation: is_a? "Returns true if class is the class of obj, or if class is one of the superclasses of obj or modules included in obj."
The Example
The example checks five values: a list of two roles, the number 42 twice, against Integer and against Numeric, the string '42' and nil. Then it compares an order of 120 with a limit that arrives as the string '500' instead of a number. It checks the limit with is_a? before comparing, then compares without the check.
Prerequisites
- Ruby. This guide uses Ruby 4.0.10.
Step 1: Write the Code
Create a file named main.rb:

p ['editor', 'viewer'].is_a?(Array)
p 42.is_a?(Integer)
p 42.is_a?(Numeric) # Numeric is a superclass of Integer
p '42'.is_a?(Integer)
p nil.is_a?(Array)
order = 120
limit = '500' # a string, where a whole number was expected
# Check the type first, so > only ever compares two numbers.
if limit.is_a?(Integer)
puts order > limit
else
puts 'limit is not an Integer'
end
p prints each result, true or false. Numeric is the parent class of both Integer and Float.
Step 2: Run It
ruby main.rb
true
true
true
false
false
limit is not an Integer
The command is the same in zsh, bash and PowerShell.
Step 3: Remove the Check
Replace main.rb with a version that compares the order with the limit without checking it:
order = 120
limit = '500' # a string, where a whole number was expected
# Without the is_a? check, > gets a string.
puts order > limit
ruby main.rb
main.rb:4:in 'Integer#>': comparison of Integer with String failed (ArgumentError)
puts order > limit
^^^^^
from main.rb:4:in '<main>'

What Happened
['editor', 'viewer']is anArray, and42is anInteger.42.is_a?(Numeric)wastrueas well, becauseNumericis the parent class ofInteger.'42'isn't anInteger, even though it looks like one: it's aString.nilisn't anArray.- The string
'500'went to theelsebranch, so>never had to compare it with a number. - Without the check,
order > limitstopped the script withcomparison of Integer with String failed (ArgumentError). Ruby printed the line that failed, with^marks underlimit.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Checking value.is_a?(Boolean) | Ruby has no Boolean class: uninitialized constant Boolean (NameError) | Check [true, false].include?(value), or is_a?(TrueClass) and is_a?(FalseClass) |
Expecting 500.0.is_a?(Integer) to be true | It's false: 500.0 is a Float | Check is_a?(Numeric) when any number will do |
Using instance_of? to accept any number | 42.instance_of?(Numeric) was false, because it matches only the exact class | Use is_a?, which matches parent classes too |
Expecting a string of digits to pass is_a?(Integer) | '42'.is_a?(Integer) is false | Convert it first: Integer('42') gave 42 |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
comparison of Integer with String failed (ArgumentError) | A value you compare with a number is a string | Check is_a?(Integer) first, or convert the string with Integer(...) |
uninitialized constant Boolean (NameError) | is_a?(Boolean), a class Ruby doesn't have | Use [true, false].include?(value) |
FAQ
What's the difference between is_a?, kind_of? and instance_of?
kind_of? is another name for is_a?: both returned true for 42 and Numeric. instance_of? matches only the exact class: 42.instance_of?(Integer) was true, but 42.instance_of?(Numeric) was false. The Ruby documentation describes it as "Returns true if obj is an instance of the given class."
What type does a JSON number have in Ruby?
JSON.parse gives a whole number as an Integer and a number with a decimal point as a Float. In my check, 500 came back as an Integer and 500.0 as a Float, so is_a?(Integer) tells the two apart.
Conclusion
is_a? tells you a value's type before you rely on it, and it matches parent classes too, so is_a?(Numeric) accepts both Integer and Float. Check values that come from outside your code, such as JSON, before you compare them or call methods on them.
Additional Resources
- is_a?, kind_of? and instance_of?, in the Ruby documentation.
- Integer and Float, in the Ruby documentation.
