Explore Modules and Mixins in Ruby with this guide on defining and using modules, including, extending, and mixing modules, and how namespaces help organize and manage code efficiently in Ruby.
1. Defining and Using Modules
What is a module in Ruby used for?
A) Defining classes
B) Grouping related methods and constants
C) Defining variables
D) Defining inheritance hierarchies
How do you define a module in Ruby?
A) module ModuleName
B) class ModuleName
C) define ModuleName
D) module = ModuleName
Which of the following is a valid way to include a module in a class?
A) class MyClass; include MyModule; end
B) class MyClass; extend MyModule; end
C) class MyClass; mixin MyModule; end
D) class MyClass; insert MyModule; end
What does the module keyword in Ruby define?
A) A class
B) A constant
C) A group of methods and constants
D) A block of code
Which method can be used to check if a class includes a particular module in Ruby?
A) .is_module?
B) .module?
C) .included_modules
D) .include?
Which of the following is NOT a characteristic of modules in Ruby?
A) They can be inherited
B) They can hold methods
C) They can hold constants
D) They cannot instantiate objects
How do you define a constant inside a module in Ruby?
A) CONSTANT_NAME = value
B) module CONSTANT_NAME = value
C) constant CONSTANT_NAME = value
D) constant value
How do you use a method from a module inside a class in Ruby?
A) method_name
B) include method_name
C) module.method_name
D) self.method_name
What is the purpose of the extend keyword when used with modules in Ruby?
A) To add methods to an object
B) To create a new class
C) To add methods to a class itself
D) To instantiate a new object
Which of the following is a valid way to define a module method that can be used outside of the module?
A) def self.method_name
B) def method_name
C) def Module.method_name
D) def module.method_name
2. Including, Extending, and Mixing Modules
What does the include keyword do when used with modules in Ruby?
A) It adds the module’s methods to the class’s instances
B) It adds the module’s methods to the class itself
C) It creates a subclass of the module
D) It replaces the class methods with module methods
What is the difference between include and extend in Ruby modules?
A) include adds methods to the class itself, and extend adds methods to instances
B) include adds methods to instances, and extend adds methods to the class
C) include only works with methods, and extend works with constants
D) There is no difference between include and extend
Which of the following would allow a module’s methods to be used as class methods in Ruby?
A) include
B) extend
C) mixin
D) prepend
What happens when you use include in a Ruby class?
A) The class gets access to the methods of the module as class methods
B) The class gets access to the methods of the module as instance methods
C) The class creates a subclass of the module
D) The class replaces its methods with those of the module
Which of the following is the correct way to mix a module into a class in Ruby?
A) class MyClass; include MyModule; end
B) module MyClass; extend MyModule; end
C) class MyClass; mix MyModule; end
D) module MyClass; include MyModule; end
How can you include multiple modules into a Ruby class?
A) Use include for each module
B) Use extend for each module
C) Use mixin for each module
D) You can only include one module at a time
What does the prepend keyword do in Ruby?
A) It includes a module before other modules or methods in the method lookup chain
B) It adds a module’s methods to the class as instance methods
C) It adds a module’s methods to the class as class methods
D) It creates a subclass of the module
Which method allows you to check if a class has included a module in Ruby?
A) .include?
B) .included?
C) .module?
D) .has_module?
What is the result of calling Module.new in Ruby?
A) It creates a new instance of a module
B) It creates a new class
C) It creates a new module
D) It defines a new constant
Which of the following allows you to use a module as a class method in Ruby?
A) include
B) extend
C) prepend
D) mixin
3. Namespaces
What is the purpose of using namespaces in Ruby?
A) To store multiple classes in a single module
B) To organize classes and modules to prevent name conflicts
C) To define classes and modules inside a class
D) To create methods inside a class
How can you define a module inside a namespace in Ruby?
A) namespace ModuleName
B) module Namespace::ModuleName
C) namespace module Namespace.ModuleName
D) module Namespace[ModuleName]
What is the result of calling Namespace::ModuleName.method_name in Ruby?
A) Accessing the method from the module in the namespace
B) Creating a new method in the namespace
C) Defining a new namespace inside the module
D) Raising an error
How do you use a class inside a module that is part of a namespace in Ruby?
A) Namespace::ModuleName::ClassName.new
B) Namespace::ClassName.new
C) ModuleName::Namespace::ClassName.new
D) ModuleName::ClassName
What is the correct way to define a constant inside a module in Ruby?
A) CONST_NAME = value
B) module CONST_NAME = value
C) namespace CONST_NAME = value
D) module CONST_NAME::value
How do you access a constant from a module within a namespace in Ruby?
A) Namespace::ModuleName::CONSTANT_NAME
B) ModuleName::Namespace::CONSTANT_NAME
C) Namespace::CONSTANT_NAME
D) ModuleName::CONSTANT_NAME
How can you ensure that a method defined in a namespace is not accessible globally?
A) Define the method within a module or class in a namespace
B) Define the method using private
C) Use the include keyword
D) Methods are always globally accessible
What is the main benefit of using namespaces in Ruby?
A) To prevent name conflicts in large codebases
B) To make all methods private by default
C) To automatically include modules
D) To add method overrides
How can you use a method from a module inside a namespace?
A) Namespace::ModuleName.method_name
B) Namespace.method_name
C) ModuleName.method_name
D) ModuleName::Namespace.method_name
Which of the following is a valid namespace declaration in Ruby?
A) module Namespace::SubNamespace
B) namespace SubNamespace
C) namespace Namespace::SubNamespace
D) module Namespace[SubNamespace]
Answers
Qno
Answer
1
B) Grouping related methods and constants
2
A) module ModuleName
3
A) class MyClass; include MyModule; end
4
C) A group of methods and constants
5
C) .included_modules
6
A) They can be inherited
7
A) CONSTANT_NAME = value
8
C) module.method_name
9
C) To add methods to a class itself
10
A) def self.method_name
11
A) It adds the module’s methods to the class’s instances
12
B) include adds methods to instances, and extend adds methods to the class
13
B) extend
14
B) The class gets access to the methods of the module as instance methods
15
A) class MyClass; include MyModule; end
16
A) Use include for each module
17
A) It includes a module before other modules or methods in the method lookup chain
18
A) .include?
19
C) It creates a new module
20
B) extend
21
B) To organize classes and modules to prevent name conflicts
22
B) module Namespace::ModuleName
23
A) Accessing the method from the module in the namespace
24
A) Namespace::ModuleName::ClassName.new
25
A) CONST_NAME = value
26
A) Namespace::ModuleName::CONSTANT_NAME
27
A) Define the method within a module or class in a namespace