module MyModule
def module_method
end
end
class Parent
def foo
end
end
class Child < Parent
include MyModule
def child_class_method
end
end
obj = Child.new
puts obj.class #### Child
foo = obj.class
puts foo.name #### Child
print foo.ancestors #### [Child, MyModule, Parent, Object, Kernel, BasicObject]
print foo.included_modules #### [MyModule, Kernel]
print foo.singleton_class? #### flase
puts foo.singleton_class.singleton_class? #### true
puts foo.include?(MyModule) #### true
puts obj.instance_of? Child #### true
puts obj.instance_of? Parent #### false
puts obj.kind_of? Child #### true
puts obj.is_a? Child #### true
puts obj.kind_of? Parent #### true
puts obj.kind_of? MyModule #### true
puts obj.kind_of? Object #### true
puts obj.kind_of? Module #### false
puts obj.kind_of? Class #### false
puts obj.kind_of? Kernel #### true
puts obj.kind_of? BasicObject #### true
puts Parent.kind_of? Class #### true
puts Parent.new.kind_of? Class #### true
print Parent.instance_methods ####
puts
print Parent.methods ####
puts
puts Parent.instance_methods – Parent.methods
print Parent.methods – Parent.instance_methods
puts obj.is_a? Child #### true ## is_a? is same ad kind_of? means if a class is present in the ancestor tree
puts obj.is_a? Parent #### true
print obj.methods #### return all the methods available in the instance along with the methods in available in the hierarchy
puts
def obj.my_method
‘its is a singleton methods’
end
puts obj.methods(false) ## return only signleton methods available to that object or we can also use
puts obj.singleton_methods ## return only signleton methods available to that object
print Child.instance_methods ## All the methods defined in class and ancestors
puts
puts Child.instance_methods(false) ## All the methods defined in class only not in the anscestor
puts ‘########### method_defined? is called on an class’
puts Child.method_defined?(:child_methods) ## check Method present inside the class
puts Child.method_defined?(:child_class_method) ## Method present inside the class
puts Parent.method_defined?(:foo) #### true, method defined inside the parent class
puts ‘######## Respond to is called on an object ###############’
puts obj.respond_to? :child_class_method #### Find if a instance methods responds to a method
puts obj.respond_to? :clone #### Find if a instance responds to a method
puts obj.respond_to? :module_method #### Find if a instance responds to a method
puts obj.respond_to? :foo #### Find if a instance methods responds to a method
puts ‘######### List of all public, private & protected methods of class #########’
puts ‘Public methods of child class’
print Child.public_methods
puts
puts ‘Private methods of child class’
print Child.private_methods
puts
print Child.protected_methods
puts ‘Public method of the class itsel’
print Child.public_methods(false)
puts ‘Private method of the class itsel’
print Child.private_methods(false)
puts ‘Protected method of the class itsel’
print Child.protected_methods(false)
puts ‘######### List of all public prive protected methods of instance #########’
print obj.public_methods
puts
print obj.public_methods
puts
print obj.protected_methods
puts (‘##### check if a method of a type is defined for class ####’)
puts Child.private_method_defined? :initialize
puts Child.public_method_defined? :new
puts Child.public_method_defined? :include?
puts Child.protected_method_defined? :new
puts obj.singleton_class.instance_methods(false) ## it will return single methods in a class
puts obj.singleton_methods ## it will return single methods in a class