0
0
Rubyprogramming~20 mins

Why modules solve multiple inheritance in Ruby - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Modules Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of including modules in Ruby
What is the output of this Ruby code when calling obj.greet?
Ruby
module A
  def greet
    "Hello from A"
  end
end

module B
  def greet
    "Hello from B"
  end
end

class MyClass
  include A
  include B
end

obj = MyClass.new
puts obj.greet
AHello from B
BHello from A
CHello from AHello from B
DNoMethodError
Attempts:
2 left
💡 Hint
Remember that the last included module's methods override earlier ones.
🧠 Conceptual
intermediate
1:30remaining
Why Ruby uses modules instead of multiple inheritance
Why does Ruby use modules to solve the problem of multiple inheritance instead of allowing classes to inherit from multiple classes?
ABecause modules allow sharing methods without the complexity of multiple class inheritance and avoid diamond problem issues.
BBecause Ruby does not support inheritance at all.
CBecause modules are faster to execute than classes.
DBecause Ruby classes can only inherit from modules, not other classes.
Attempts:
2 left
💡 Hint
Think about method conflicts and complexity in multiple inheritance.
🔧 Debug
advanced
1:30remaining
Identify the error in module inclusion
What error will this Ruby code produce and why?
Ruby
module M
  def hello
    "Hi"
  end
end

class C
  include M
  include M
end

c = C.new
puts c.hello
ANameError: undefined method 'hello'
BTypeError: wrong argument type Module (expected Class)
CNo error, outputs "Hi"
DArgumentError: duplicate include
Attempts:
2 left
💡 Hint
Including the same module twice is allowed but has no extra effect.
Predict Output
advanced
2:00remaining
Method lookup order with modules and inheritance
What is the output of this Ruby code?
Ruby
module M1
  def speak
    "M1 speaks"
  end
end

module M2
  def speak
    "M2 speaks"
  end
end

class Parent
  def speak
    "Parent speaks"
  end
end

class Child < Parent
  include M1
  include M2
end

puts Child.new.speak
AParent speaks
BM2 speaks
CM1 speaks
DNoMethodError
Attempts:
2 left
💡 Hint
Modules included last override earlier modules and superclass methods.
🧠 Conceptual
expert
2:30remaining
How modules solve the diamond problem in Ruby
In multiple inheritance, the diamond problem occurs when a class inherits from two classes that both inherit from a common superclass. How do Ruby modules help avoid this problem?
ARuby requires explicit method overrides to resolve diamond problem manually.
BModules prevent inheritance entirely, so diamond problem cannot occur.
CRuby duplicates methods from modules to each class, so no shared superclass exists.
DModules are mixed in linearly, so Ruby uses a method lookup path that avoids ambiguity by checking modules in reverse inclusion order before superclass.
Attempts:
2 left
💡 Hint
Think about how Ruby searches for methods when modules are included.