Bird
0
0

You want to add logging behavior to multiple unrelated classes without using multiple inheritance. How can you do this in Ruby?

hard📝 Application Q8 of 15
Ruby - Inheritance
You want to add logging behavior to multiple unrelated classes without using multiple inheritance. How can you do this in Ruby?
AMake all classes inherit from a common Logging class
BDefine logging methods inside each class separately
CUse global variables to share logging methods
DCreate a module with logging methods and include it in each class
Step-by-Step Solution
Solution:
  1. Step 1: Recognize Ruby's single inheritance limitation

    Ruby allows only one parent class, so multiple inheritance is not possible.
  2. Step 2: Use modules to share behavior

    Modules can be included in any class to add shared methods like logging without inheritance.
  3. Final Answer:

    Create a module with logging methods and include it in each class -> Option D
  4. Quick Check:

    Modules add shared behavior without multiple inheritance [OK]
Quick Trick: Use modules to share code across unrelated classes [OK]
Common Mistakes:
  • Trying to inherit from multiple classes for logging
  • Using global variables instead of modules
  • Duplicating code inside each class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes