0
0
Rubyprogramming~5 mins

Namespacing with modules in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the main purpose of using modules for namespacing in Ruby?
Modules group related classes, methods, or constants to avoid name conflicts and organize code clearly.
Click to reveal answer
beginner
How do you define a module named MathTools in Ruby?
Use module MathTools # code here end to define the module.
Click to reveal answer
beginner
How do you access a class Calculator inside a module MathTools?
Use the scope resolution operator: MathTools::Calculator.
Click to reveal answer
intermediate
What happens if two classes have the same name but are in different modules?
They are treated as different classes because the module names keep them separate, avoiding conflicts.
Click to reveal answer
intermediate
Can modules be nested inside other modules in Ruby? Why would you do this?
Yes, nesting modules helps organize code hierarchically and create more specific namespaces.
Click to reveal answer
Which symbol is used to access a class inside a module in Ruby?
A::
B.
C#
D->
What keyword starts the definition of a module in Ruby?
Aclass
Bmodule
Cdef
Dnamespace
Why use modules for namespacing?
ATo avoid name clashes and organize code
BTo create multiple inheritance
CTo define variables
DTo run code faster
If you have module A; class B; end; end, how do you create an instance of class B?
AB.new
BA.B.new
Cnew A::B
DA::B.new
Can modules contain methods in Ruby?
AYes, but only instance methods
BNo, only classes can have methods
CYes, modules can have methods
DOnly private methods
Explain how modules help prevent name conflicts in Ruby programs.
Think about how two classes with the same name can exist without problems.
You got /4 concepts.
    Describe how to define and use a class inside a nested module structure.
    Imagine folders inside folders to organize files.
    You got /4 concepts.