Created
June 23, 2021 06:21
-
-
Save JuzerShakir/de7c2f3fa0c901df7b3b871f07691f7d to your computer and use it in GitHub Desktop.
A look into hierarchical tree of classes and modules in ruby
This file contains 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
# Date Created: 16 June 2021 | |
# Ruby Version 3.0.0p | |
class Abcd;end | |
# ------------------ Superclass --------------- | |
Abcd.superclass # => Object | |
Abcd.superclass.superclass # => BasicObject | |
Abcd.superclass.superclass.superclass # => nil | |
# as BasicObject is top level class that doesn't have any parent class | |
# ------------------ Ancestors ---------------- | |
Abcd.ancestors # => [Abcd, Object, Kernel, BasicObject] | |
# => "Superclass on 'Abcd.class':" | |
Abcd.class # => Class | |
Abcd.class.superclass # => Module | |
Abcd.class.superclass.superclass # => Object | |
Abcd.class.superclass.superclass.superclass # => BasicObject | |
# => "Ancestors on 'Abcd.class':" | |
Abcd.class.ancestors # => [Class, Module, Object, Kernel, BasicObject] | |
Abcd.is_a? Class # => true | |
String.is_a? Class # => true, Abcd is a class just like a String and both are instances of class Class | |
Abcd.instance_of? Class # => true | |
Class.is_a? Class # => true | |
Module.is_a? Class # true, even Object and BasicObject are class | |
Kernel.is_a? Module # => true | |
Enumerable.is_a? Module # true | |
# => retunrs class name that initiated an objecc. | |
Abcd.new.class # Abcd | |
' '.class # => String | |
' '.class.ancestors # => [String, Comparable, Object, Kernel, BasicObject] | |
String.class.ancestors # => [Class, Module, Object, Kernel, BasicObject] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment