Last active
December 11, 2015 05:38
-
-
Save jehoshua02/4553263 to your computer and use it in GitHub Desktop.
Difference between `is_a?` and `instance_of?`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So use
is_a?when any subclass will do. Useinstance_of?when you want a specific type.