Recall & Review
beginner
What is the basic syntax to declare a module in Ruby?
Use the
module keyword followed by the module name and end. For example:<br>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:<br>
module Outer
module Inner
# code
end
end
endClick to reveal answer
Which keyword starts a module declaration in Ruby?
✗ Incorrect
The
module keyword is used to declare a module in Ruby.What keyword ends a module declaration in Ruby?
✗ Incorrect
Ruby uses
end to close blocks including modules.Can a Ruby module contain instance 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?
✗ 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?
✗ 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.