What is the basic syntax to declare a module in Ruby?
Use the module keyword followed by the module name and end. For example:
module MyModule
# code here
end
Click to reveal answer
beginner
Can a Ruby module contain methods and constants?
Yes, a Ruby module can contain methods, constants, and even other modules or classes. It acts like a container for related code.
Click to reveal answer
beginner
How do you define a method inside a Ruby module?
Inside the module block, define methods just like in a class using <code>def method_name</code>. For example:<br><pre>module Greetings
def hello
puts 'Hello!'
end
end</pre>
Click to reveal answer
intermediate
What is the purpose of using modules in Ruby?
Modules group related methods and constants together. They help organize code and can be mixed into classes to share behavior without inheritance.
Click to reveal answer
intermediate
Is it possible to nest modules inside other modules in Ruby?
Yes, Ruby allows nesting modules inside other modules to create a hierarchy or namespace. For example:
module Outer
module Inner
# code
end
end
end
Click to reveal answer
Which keyword starts a module declaration in Ruby?
Apackage
Bmodule
Cdef
Dclass
✗ Incorrect
The module keyword is used to declare a module in Ruby.
What keyword ends a module declaration in Ruby?
Aend
Bstop
Cclose
Dfinish
✗ Incorrect
Ruby uses end to close blocks including modules.
Can a Ruby module contain instance variables?
AModules can have instance variables but they behave differently
BYes, like classes
CModules can have class variables but not instance variables
DNo, modules cannot have any variables
✗ Incorrect
Modules can have instance variables, but they belong to the module itself, not to instances like in classes.
How do you call a method defined inside a module without including it in a class?
Anew module_name.method_name()
Bmethod_name()
Cmodule_name.method_name
DYou cannot call it without including
✗ Incorrect
You can call module methods directly using the module name if they are defined as module methods (e.g., with self.method_name).
What is a common use of modules in Ruby?
ATo create objects
BTo replace classes
CTo store global variables
DTo share reusable code across classes
✗ Incorrect
Modules are often used to share reusable methods and constants across multiple classes.
Explain how to declare a module in Ruby and what it can contain.
Think about the structure and what you can put inside the module.
You got /6 concepts.
Describe why and how you would use modules in Ruby programming.
Consider how modules help avoid repeating code.
You got /4 concepts.
Practice
(1/5)
1. What is the correct way to declare a module named MathTools in Ruby?
easy
A. def MathTools
# code here
end
B. module MathTools
# code here
end
C. class MathTools
# code here
end
D. package MathTools {
# code here
}
Solution
Step 1: Recognize Ruby module syntax
In Ruby, modules are declared using the keyword module followed by the module name and end.
Step 2: Identify the correct declaration
module MathTools
# code here
end uses module MathTools ... end, which is the correct syntax for a module.
Final Answer:
module MathTools
# code here
end -> Option B
Quick Check:
Ruby modules use module Name ... end [OK]
Hint: Modules start with 'module' and end with 'end' [OK]
Common Mistakes:
Using 'class' instead of 'module'
Using 'def' which is for methods
Using non-Ruby syntax like 'package'
2. Which of the following is a syntax error when declaring a module in Ruby?
easy
A. module Tools {
def help
end
}
B. module Tools
def self.help
end
end
C. module Tools
end
D. module Tools
CONSTANT = 10
end
Solution
Step 1: Check Ruby module block delimiters
Ruby modules use module ... end without braces { }.
Step 2: Identify invalid syntax
module Tools {
def help
end
} uses braces { } which is invalid syntax for module declaration in Ruby.
Final Answer:
module Tools {
def help
end
} -> Option A
Quick Check:
Ruby modules use 'module ... end', not braces [OK]
Hint: Ruby modules never use curly braces for blocks [OK]
Common Mistakes:
Using curly braces instead of 'end'
Missing 'end' keyword
Confusing module with class syntax
3. What will be the output of the following Ruby code?
module Greetings
def self.say_hello
"Hello!"
end
end
puts Greetings.say_hello
medium
A. Hello!
B. Greetings.say_hello
C. NoMethodError
D. SyntaxError
Solution
Step 1: Understand module method definition
The method say_hello is defined as a module method using self., so it can be called on the module directly.
Step 2: Evaluate the method call output
Calling Greetings.say_hello returns the string "Hello!", which is printed by puts.
Final Answer:
Hello! -> Option A
Quick Check:
Module method call returns string output [OK]
Hint: Module methods use self. and can be called directly [OK]
Common Mistakes:
Trying to call method without self. prefix
Expecting instance method behavior
Confusing syntax errors with method calls
4. Identify the error in the following Ruby module declaration:
module Tools
def help
"Help message"
end
end
Tools.help
medium
A. No error, code runs fine
B. Missing 'end' keyword for module
C. Method 'help' should be defined with 'self.' to call on module
D. Module name should be lowercase
Solution
Step 1: Check method definition inside module
The method help is defined as an instance method, but called on the module directly.
Step 2: Understand method call context
To call Tools.help, help must be a module method defined with self.help.
Final Answer:
Method 'help' should be defined with 'self.' to call on module -> Option C
Quick Check:
Module methods need 'self.' prefix to be called on module [OK]
Hint: Use 'def self.method' for module-level methods [OK]
Common Mistakes:
Defining instance methods but calling on module
Forgetting 'self.' prefix for module methods
Assuming lowercase module names are valid
5. Given the following nested module declarations, which is the correct way to define a module Inner inside module Outer in Ruby?
module Outer
# Your code here
end
hard
A. module Outer
class Inner
end
end
B. module Outer::Inner
end
C. module Outer.Inner
end
D. module Outer
module Inner
end
end
Solution
Step 1: Understand nested module syntax
In Ruby, nested modules can be declared by placing one module inside another.
Step 2: Identify correct nested declaration
module Outer
module Inner
end
end correctly nests module Inner inside module Outer using indentation and end keywords.
Final Answer:
module Outer
module Inner
end
end -> Option D
Quick Check:
Nested modules use nested 'module ... end' blocks [OK]
Hint: Nest modules by placing one inside another with 'module' [OK]
Common Mistakes:
Using dot notation instead of :: for nested modules
Using class instead of module for nested module
Incorrect syntax like dot (.) in module names
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.