How do I check if a variable is an instance of a class?

Viewed 86181

In Java, you can do instanceof. Is there a Ruby equivalent?

5 Answers

Adding another answer for completeness. Sometimes, particularly during testing, we may not want to access another class by type, so given a Hash:

h = { one: 'Val 1' }

instead of writing:

h.is_a? Hash # true

we can write:

h.class.name == 'Hash' # true
Related