Bird
0
0

Choose the correct code snippet.

hard📝 Application Q15 of 15
Ruby - Modules and Mixins
Given two modules Walk and Talk with methods walk and speak respectively, how can you create a class Person that uses both modules as mixins and calls both methods on an instance? Choose the correct code snippet.
Aclass Person extend Walk extend Talk end p = Person.new puts p.walk puts p.speak
Bclass Person include Walk Talk end p = Person.new puts p.walk puts p.speak
Cclass Person include Walk include Talk end p = Person.new puts p.walk puts p.speak
Dclass Person use Walk use Talk end p = Person.new puts p.walk puts p.speak
Step-by-Step Solution
Solution:
  1. Step 1: Understand including multiple modules

    You can include multiple modules by calling include multiple times inside the class.
  2. Step 2: Verify method calls on instance

    Including modules adds their methods as instance methods, so p.walk and p.speak work.
  3. Final Answer:

    class Person include Walk include Talk end p = Person.new puts p.walk puts p.speak -> Option C
  4. Quick Check:

    Include modules separately for instance methods [OK]
Quick Trick: Include each module separately for instance methods [OK]
Common Mistakes:
  • Using extend instead of include for instance methods
  • Trying to include multiple modules in one line
  • Using non-existent keyword 'use'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes