0
0
Rubyprogramming~10 mins

Namespacing with modules in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Namespacing with modules
Define Module
Define Class or Method inside Module
Use Module::Class or Module::Method
Access namespaced code
Avoid name conflicts
This flow shows how a module is defined to group code, then how classes or methods inside it are accessed using the module name to avoid conflicts.
Execution Sample
Ruby
module Greetings
  def self.say_hello
    "Hello!"
  end
end

puts Greetings.say_hello
Defines a module with a method and calls it using the module name to show namespacing.
Execution Table
StepActionEvaluationResult
1Define module GreetingsModule Greetings createdModule Greetings available
2Define method say_hello inside GreetingsMethod say_hello addedMethod accessible as Greetings.say_hello
3Call Greetings.say_helloMethod executed"Hello!" returned
4Print returned valueOutput to consoleHello!
💡 Program ends after printing the namespaced method output.
Variable Tracker
VariableStartAfter Step 3Final
GreetingsModule definedModule with method say_helloModule with method say_hello
say_helloNot definedMethod defined inside GreetingsMethod defined inside Greetings
Key Moments - 2 Insights
Why do we write Greetings.say_hello instead of just say_hello?
Because say_hello is inside the Greetings module, we must use the module name to access it, as shown in step 3 of the execution table.
Can two modules have methods with the same name?
Yes, modules create separate namespaces, so methods with the same name in different modules do not conflict, avoiding confusion.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of calling Greetings.say_hello at step 3?
Anil
B"Hello!"
CError: method not found
D"Goodbye!"
💡 Hint
Check the 'Evaluation' and 'Result' columns at step 3 in the execution table.
At which step is the method say_hello defined inside the module?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column describing method definition in the execution table.
If we define another module with a method named say_hello, what happens?
AIt overwrites Greetings.say_hello
BIt causes a syntax error
CIt creates a separate method without conflict
DIt deletes the first module
💡 Hint
Refer to the key moment about modules creating separate namespaces.
Concept Snapshot
module ModuleName
  def self.method_name
    # code
  end
end

Use ModuleName.method_name to call.
Modules group code to avoid name conflicts.
Full Transcript
This example shows how to use modules in Ruby to create namespaces. First, a module named Greetings is defined. Inside it, a method say_hello is defined as a module method. To call this method, we use the full name Greetings.say_hello. This avoids conflicts if other parts of the program have methods with the same name. The execution table traces defining the module, adding the method, calling it, and printing the result. Variables track the module and method existence. Key moments clarify why the module name is needed to access the method and how modules prevent name clashes. The quiz tests understanding of method calls, definition steps, and namespace separation. The snapshot summarizes syntax and usage for quick reference.