Complete the code to call the method defined in the class.
class Animal def speak 'Hello' end end animal = Animal.new puts animal.[1]
The method speak is defined in the Animal class. Calling animal.speak runs that method and prints 'Hello'.
Complete the code to call the method from the module included in the class.
module Talk def speak 'Hi from module' end end class Person include Talk end p = Person.new puts p.[1]
The Person class includes the Talk module, which defines the speak method. So calling p.speak uses the module's method.
Fix the error by completing the code to call the method from the superclass.
class Vehicle def start 'Starting engine' end end class Car < Vehicle end car = Car.new puts car.[1]
The Car class inherits from Vehicle. The start method is defined in Vehicle, so car.start calls that method.
Fill both blanks to complete the method lookup chain with module and superclass methods.
module M def greet 'Hello from M' end end class A def greet 'Hello from A' end end class B < A include M end b = B.new puts b.[1] puts b.class.superclass.instance_method(:[2]).bind(b).call if b.class.superclass.method_defined?(:[2])
The object b calls greet from module M first. Then it calls greet from its superclass A using b.class.superclass.instance_method(:greet).bind(b).call.
Fill all three blanks to complete the method lookup chain with class, module, and superclass methods.
module M1 def action 'Action from M1' end end module M2 def action 'Action from M2' end end class Parent def action 'Action from Parent' end end class Child < Parent include M1 include M2 def action 'Action from Child' end end c = Child.new puts c.[1] puts c.singleton_class.ancestors[2].instance_method(:action).bind(c).call puts c.class.superclass.instance_method(:[2]).bind(c).call if c.class.superclass.method_defined?(:[3])
The Child class defines action, which is called first. Then the method from the last included module M2 is called via singleton_class.ancestors[2]. Finally, the action method from the superclass Parent is called.