Bird
0
0

Identify the error in this code:

medium📝 Debug Q6 of 15
Ruby - Modules and Mixins
Identify the error in this code:
module M
  def greet
    "Hello from M, " + super
  end
end

class C
  prepend M
end

puts C.new.greet
ANoMethodError because C does not define greet method
BSyntaxError due to missing method end
CTypeError because prepend is used incorrectly
DNo error, prints 'Hello from M, '
Step-by-Step Solution
Solution:
  1. Step 1: Check method definitions

    Module M defines greet calling super, but class C does not define greet.
  2. Step 2: Understand super call behavior

    Calling super in M's greet looks for a method in C or ancestors, but none exists, causing NoMethodError.
  3. Final Answer:

    NoMethodError because C does not define greet method -> Option A
  4. Quick Check:

    Super without method below causes NoMethodError [OK]
Quick Trick: Super requires method in next lookup, else NoMethodError [OK]
Common Mistakes:
  • Assuming super returns empty string
  • Thinking prepend causes syntax error
  • Ignoring missing method in class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes