Challenge - 5 Problems
Ruby Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Why does Ruby use single inheritance?
Ruby supports only single inheritance. What is the main reason for this design choice?
Attempts:
2 left
💡 Hint
Think about how multiple inheritance can cause confusion in some languages.
✗ Incorrect
Ruby uses single inheritance to keep the class hierarchy simple and avoid problems like the diamond problem that occur with multiple inheritance. Instead, Ruby uses modules to share code.
🧠 Conceptual
intermediate2:00remaining
How does Ruby achieve code reuse without multiple inheritance?
Since Ruby allows only single inheritance, how does it let programmers reuse code from multiple sources?
Attempts:
2 left
💡 Hint
Think about a way to add methods to a class without inheritance.
✗ Incorrect
Ruby uses modules and mixins to add shared methods to classes. This allows code reuse without the complexity of multiple inheritance.
❓ Predict Output
advanced2:00remaining
Output of Ruby inheritance and module inclusion
What will be the output of this Ruby code?
Ruby
module M def greet "Hello from M" end end class A def greet "Hello from A" end end class B < A include M end puts B.new.greet
Attempts:
2 left
💡 Hint
Remember that included modules override methods from the superclass.
✗ Incorrect
In Ruby, when a module is included in a class, its methods override those from the superclass. So B's greet method comes from module M.
❓ Predict Output
advanced2:00remaining
Method lookup order in Ruby with single inheritance and modules
What will this Ruby code print?
Ruby
module M1 def info "M1" end end module M2 def info "M2" end end class Parent def info "Parent" end end class Child < Parent include M1 include M2 end puts Child.new.info
Attempts:
2 left
💡 Hint
The last included module has the highest priority in method lookup.
✗ Incorrect
Ruby's method lookup checks the last included module first, so M2's info method is called.
🧠 Conceptual
expert3:00remaining
Why is multiple inheritance avoided in Ruby but allowed in some other languages?
Why does Ruby avoid multiple inheritance while languages like C++ allow it?
Attempts:
2 left
💡 Hint
Think about the problems multiple inheritance can cause and how Ruby solves them differently.
✗ Incorrect
Ruby avoids multiple inheritance to prevent issues like the diamond problem and method conflicts. Instead, it uses modules to mix in behavior, providing flexibility without complexity.