0
0
Rubyprogramming~20 mins

Why single inheritance in Ruby - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why does Ruby use single inheritance?
Ruby supports only single inheritance. What is the main reason for this design choice?
ABecause Ruby only allows inheritance from built-in classes
BBecause Ruby does not support classes at all
CTo force programmers to use only modules for all code reuse
DTo keep the language simple and avoid complexity from multiple inheritance
Attempts:
2 left
💡 Hint
Think about how multiple inheritance can cause confusion in some languages.
🧠 Conceptual
intermediate
2: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?
ABy using modules and mixins to include shared behavior
BBy copying code manually between classes
CBy using multiple inheritance secretly behind the scenes
DBy disallowing any code reuse
Attempts:
2 left
💡 Hint
Think about a way to add methods to a class without inheritance.
Predict Output
advanced
2: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
AHello from M
BHello from A
CNoMethodError
DSyntaxError
Attempts:
2 left
💡 Hint
Remember that included modules override methods from the superclass.
Predict Output
advanced
2: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
AParent
BM2
CM1
DNoMethodError
Attempts:
2 left
💡 Hint
The last included module has the highest priority in method lookup.
🧠 Conceptual
expert
3: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?
ABecause Ruby does not support classes at all, only modules
BBecause Ruby is an older language and never updated to support multiple inheritance
CTo prevent complexity and ambiguity in method resolution, using modules instead for flexibility
DBecause Ruby only supports inheritance from a single built-in class
Attempts:
2 left
💡 Hint
Think about the problems multiple inheritance can cause and how Ruby solves them differently.