Complete the code to include a module in a class.
module Greetings def hello puts 'Hello!' end end class Person [1] Greetings end p = Person.new p.hello
In Ruby, include is used to mix in a module's methods as instance methods in a class.
Complete the code to call a method from a module mixed into a class.
module Logger def log(message) puts "Log: #{message}" end end class App include Logger def run [1]('Application started') end end app = App.new app.run
puts instead of the module method log.The method log is defined in the module and becomes available as an instance method in the class.
Fix the error in the code to correctly use multiple modules in a class.
module A def greet puts 'Hello from A' end end module B def greet puts 'Hello from B' end end class Person include A [1] B end p = Person.new p.greet
To mix in multiple modules as instance methods, use include for each module.
Fill both blanks to define a module and include it in a class to solve multiple inheritance.
module [1] def info puts 'Info from module' end end class Device [2] InfoModule end d = Device.new d.info
Define the module with the correct name and include it in the class to add instance methods.
Fill all three blanks to create two modules and include both in a class to simulate multiple inheritance.
module [1] def feature_a puts 'Feature A' end end module [2] def feature_b puts 'Feature B' end end class Gadget [3] FeatureA [3] FeatureB end g = Gadget.new g.feature_a g.feature_b
Define two modules and include both in the class to add their methods as instance methods, simulating multiple inheritance.