0
0
Rubyprogramming~10 mins

Why modules solve multiple inheritance in Ruby - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to include a module in a class.

Ruby
module Greetings
  def hello
    puts 'Hello!'
  end
end

class Person
  [1] Greetings
end

p = Person.new
p.hello
Drag options to blanks, or click blank then click option'
Ainclude
Bextend
Cinherit
Drequire
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extend' instead of 'include' to add instance methods.
2fill in blank
medium

Complete the code to call a method from a module mixed into a class.

Ruby
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
Drag options to blanks, or click blank then click option'
Alog
Bprint
Cputs
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call puts instead of the module method log.
3fill in blank
hard

Fix the error in the code to correctly use multiple modules in a class.

Ruby
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
Drag options to blanks, or click blank then click option'
Arequire
Binclude
Cextend
Dinherit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extend' for the second module causing method to be unavailable as instance method.
4fill in blank
hard

Fill both blanks to define a module and include it in a class to solve multiple inheritance.

Ruby
module [1]
  def info
    puts 'Info from module'
  end
end

class Device
  [2] InfoModule
end

d = Device.new
d.info
Drag options to blanks, or click blank then click option'
AInfoModule
Binclude
Cextend
DModuleInfo
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extend' instead of 'include' to add instance methods.
5fill in blank
hard

Fill all three blanks to create two modules and include both in a class to simulate multiple inheritance.

Ruby
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
Drag options to blanks, or click blank then click option'
AFeatureA
BFeatureB
Cinclude
Dextend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extend' instead of 'include' causing methods to be class methods.