Created
December 6, 2010 06:08
-
-
Save deepak/729939 to your computer and use it in GitHub Desktop.
question regarding is_a? and kind_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
| # running on irb | |
| # why is is_a? and kind_of methods work as expected on instances and not on the class itself | |
| # --- bcuz Foo.class is a Class not a String | |
| # how to differentiate between inheritance and composition relationship | |
| module Baz | |
| end | |
| class Bar < String | |
| end | |
| class Foo < Bar | |
| include Baz | |
| end | |
| f = Foo.new | |
| f.is_a? String #true | |
| f.kind_of? String #true | |
| Foo.kind_of? String #false | |
| Foo.is_a? String #false | |
| Foo.ancestors | |
| # [Foo, Baz, Bar, String, Enumerable, Comparable, Object, Kernel] | |
| # thanks to @xxxxxxx and @krainboltgreene pointing it out on #ruby irc | |
| # TODO: how to differentiate between inheritance and composition relationship? | |
| Foo < Bar # true | |
| Foo < Baz # true | |
| Foo.ancestors.include? String #true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment