Bird
0
0

How would you define this?

hard📝 Application Q8 of 15
Ruby - Modules and Mixins
You want to create a module MathOps with a module method double that doubles a number, and also allow classes to include MathOps to get an instance method triple that triples a number. How would you define this?
Amodule MathOps def double(x) x * 2 end def self.triple(x) x * 3 end end
Bmodule MathOps def self.double(x) x * 2 end def triple(x) x * 3 end end
Cmodule MathOps def self.double(x) x * 2 end def self.triple(x) x * 3 end end
Dmodule MathOps def double(x) x * 2 end def triple(x) x * 3 end end
Step-by-Step Solution
Solution:
  1. Step 1: Define module method for doubling

    Use def self.double(x) to create a module method callable on MathOps.
  2. Step 2: Define instance method for tripling

    Define def triple(x) as an instance method to be available when included in classes.
  3. Final Answer:

    Use self.double for module method and triple without self for instance method -> Option B
  4. Quick Check:

    Module method with self, instance method without = A [OK]
Quick Trick: Use self for module methods, normal defs for instance methods [OK]
Common Mistakes:
  • Defining both methods as module methods
  • Defining both as instance methods
  • Mixing up self usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes