Skip to content

Instantly share code, notes, and snippets.

@deepak
Created December 6, 2010 06:08
Show Gist options
  • Select an option

  • Save deepak/729939 to your computer and use it in GitHub Desktop.

Select an option

Save deepak/729939 to your computer and use it in GitHub Desktop.
question regarding is_a? and kind_of?
# 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