Skip to content

Instantly share code, notes, and snippets.

@jehoshua02
Last active December 11, 2015 05:38
Show Gist options
  • Select an option

  • Save jehoshua02/4553263 to your computer and use it in GitHub Desktop.

Select an option

Save jehoshua02/4553263 to your computer and use it in GitHub Desktop.
Difference between `is_a?` and `instance_of?`
require 'test/unit'
class TestInstanceOf < Test::Unit::TestCase
def test_string_is_a_string
assert "string".is_a? String
end
def test_string_instance_of_string
assert "string".instance_of? String
end
def test_string_is_a_object
assert "string".is_a? Object
end
def test_string_not_instance_of_object
# found this interesting. This could be useful where you want more strict check.
assert_equal false, "string".instance_of? Object
end
end
@jehoshua02
Copy link
Copy Markdown
Author

So use is_a? when any subclass will do. Use instance_of? when you want a specific type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment