Bird
0
0

Given nested modules Company::Department and a class Employee inside Department, how do you define a method info inside Employee that returns "Employee Info" and call it?

hard📝 Application Q9 of 15
Ruby - Modules and Mixins
Given nested modules Company::Department and a class Employee inside Department, how do you define a method info inside Employee that returns "Employee Info" and call it?
Amodule Company module Department class Employee def info 'Employee Info' end end end end puts Company::Department::Employee.new.info
Bmodule Company module Department class Employee def self.info 'Employee Info' end end end end puts Company.Department::Employee.info
Cmodule Company module Department class Employee def info 'Employee Info' end end end end puts Company.Department.Employee.info
Dmodule Company module Department class Employee def self.info 'Employee Info' end end end end puts Company.Department.Employee.new.info
Step-by-Step Solution
Solution:
  1. Step 1: Define instance method info inside Employee class

    Method info returns 'Employee Info' and is an instance method.
  2. Step 2: Call method on new instance using correct module syntax

    Use Company::Department::Employee.new.info to call instance method.
  3. Final Answer:

    module Company\n module Department\n class Employee\n def info\n 'Employee Info'\n end\n end\n end\nend\n\nputs Company::Department::Employee.new.info -> Option A
  4. Quick Check:

    Nested modules and instance method call [OK]
Quick Trick: Use :: for nested modules and new instance for instance methods [OK]
Common Mistakes:
  • Calling instance method as class method
  • Using dot instead of ::
  • Wrong method call syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes