tests=[Class,Module,Kernel,Object,BasicObject]puts"checking class and ancestors for #{tests}"tests.eachdo|x|puts"Ancestors of #{x} is #{x.ancestors}"puts"The class of #{x} is #{x.class}"endputs""defcheck_is_a(a,b)ifnota.is_a?bputs"#{a} is not a #{b}"endendputs"checking is_a relation for #{tests}"tests.eachdo|x|tests.eachdo|y|check_is_a(x,y)endend
moduleTestModuledeffunc()puts"value is @value"endendbeginobj=TestModule.newobj.func()rescueException=>eputs"Module new got an exception: #{e.class}"end
classBaseClassdefcall_func()puts"base called"endendclassMixInClass<BaseClassincludeTestModuledefinitialize(value)@value=valueenddefcall_func()superputs"called in child"enddeffunc()superputs"module func called"endendobj=MixInClass.new("mixin")obj.func()# module method calledobj.call_func()# virtual method called which will call it's ancestor by super
moduleBaseModule1deffunc()puts"called in BaseModule1"enddeffunc1()puts"unique func in module1"endendmoduleBaseModule2deffunc()puts"called in BaseModule2"enddeffunc2()puts"unique func in module2"endendclassBaseClassdeffunc()puts"called in base class"endendclassChild<BaseClassincludeBaseModule1includeBaseModule2endobj=Child.newobj.funcobj.func1obj.func2
moduleBaseModuledefinitialize(value)@value=valueenddeffunc()puts"called with value = #{@value}"endendclassTest1includeBaseModuledefinitialize(value)super(value)endendclassTest2includeBaseModuledefshow()puts"my value is: #{@value}"endendclassTest3includeBaseModuledefinitialize(value)@myvalue=valueenddefshow()puts"called with myvalue = #{@myvalue}"endendobj1=Test1.new("test1")obj1.func()obj2=Test2.new("test2")obj2.func()obj2.show()obj3=Test3.new("test3")obj3.func()obj3.show()
moduleBaseModuledefinitialize(value)@value=valueenddeffunc()puts"called with value = #{@value}"endendclassBaseClassdefinitialize(value)@baseValue=valueenddefshow()puts"my basevalue=#{@baseValue}"endendclassTest<BaseClassincludeBaseModuledefinitialize(value)super(value)endendobj=Test.new("test1")obj.funcobj.show