Bird
0
0

Identify the error in this Ruby code that tries to use a module for multiple inheritance:

medium📝 Debug Q14 of 15
Ruby - Modules and Mixins
Identify the error in this Ruby code that tries to use a module for multiple inheritance:
module Swimmable
  def swim
    "Swimming"
  end
end

class Animal
  include Swimmable
  include Flyable
end
AModules cannot define instance methods.
BYou cannot include more than one module in a class.
CThe module Flyable is not defined, causing a NameError.
DThe include keyword is misspelled.
Step-by-Step Solution
Solution:
  1. Step 1: Check module definitions

    The code defines Swimmable but does not define Flyable.
  2. Step 2: Understand include behavior

    Including an undefined module like Flyable causes a NameError because Ruby cannot find it.
  3. Final Answer:

    The module Flyable is not defined, causing a NameError. -> Option C
  4. Quick Check:

    Undefined module inclusion causes NameError = A [OK]
Quick Trick: Always define modules before including them [OK]
Common Mistakes:
  • Thinking multiple includes are not allowed
  • Believing modules can't have instance methods
  • Ignoring undefined module errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes