Skip to main content

How to Check a Value's Type in Ruby with is_a?

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<value>.is_a?(<class>)
PartWhat 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 resulttrue 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:

main.rb, which prints is_a? for a list of two roles, 42 against Integer and Numeric, the string 42 and nil, then compares an order of 120 with the limit only when is_a? says the limit is an Integer.

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​

Terminal
ruby main.rb
Output
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:

main.rb
order = 120
limit = '500' # a string, where a whole number was expected
# Without the is_a? check, > gets a string.
puts order > limit
Terminal
ruby main.rb
Output
main.rb:4:in 'Integer#>': comparison of Integer with String failed (ArgumentError)

puts order > limit
^^^^^
from main.rb:4:in '<main>'

A zsh terminal where ruby main.rb prints true three times, false twice and limit is not an Integer; for the changed file it stops with comparison of Integer with String failed (ArgumentError).

What Happened​

  • ['editor', 'viewer'] is an Array, and 42 is an Integer.
  • 42.is_a?(Numeric) was true as well, because Numeric is the parent class of Integer.
  • '42' isn't an Integer, even though it looks like one: it's a String. nil isn't an Array.
  • The string '500' went to the else branch, so > never had to compare it with a number.
  • Without the check, order > limit stopped the script with comparison of Integer with String failed (ArgumentError). Ruby printed the line that failed, with ^ marks under limit.

Common Mistakes​

MistakeWhat happensDo 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 trueIt's false: 500.0 is a FloatCheck is_a?(Numeric) when any number will do
Using instance_of? to accept any number42.instance_of?(Numeric) was false, because it matches only the exact classUse is_a?, which matches parent classes too
Expecting a string of digits to pass is_a?(Integer)'42'.is_a?(Integer) is falseConvert it first: Integer('42') gave 42

Troubleshooting​

SymptomLikely causeFix
comparison of Integer with String failed (ArgumentError)A value you compare with a number is a stringCheck is_a?(Integer) first, or convert the string with Integer(...)
uninitialized constant Boolean (NameError)is_a?(Boolean), a class Ruby doesn't haveUse [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​