Bird
Raised Fist0
Rubyprogramming~10 mins

Module declaration syntax in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Module declaration syntax
Start
↓
Declare module with 'module Name'
↓
Add methods or constants inside module
↓
End module with 'end'
↓
Module ready to use
This flow shows how to declare a module by starting with 'module', adding content, and ending with 'end'.
Execution Sample
Ruby
module Greetings
  def self.say_hello
    "Hello!"
  end
end
This code declares a module named Greetings with a method say_hello that returns a greeting string.
Execution Table
StepCode LineActionResult
1module GreetingsStart module declarationModule Greetings created
2def self.say_helloDefine module method say_helloMethod say_hello added to Greetings
3"Hello!"Method returns stringReturn value set to 'Hello!'
4endEnd method definitionMethod say_hello complete
5endEnd module declarationModule Greetings complete
💡 Module declaration ends after 'end' keyword, module is ready to be used.
Variable Tracker
VariableStartAfter Step 2After Step 5
Greetingsundefinedmodule object createdmodule object with method say_hello
Key Moments - 2 Insights
Why do we use 'self.' before method name inside a module?
Using 'self.' makes the method a module method (like a class method), so it can be called directly on the module (see step 2 in execution_table). Without 'self.', the method would be an instance method and not callable on the module itself.
What does the 'end' keyword do in module declaration?
The 'end' keyword closes the module declaration (see step 5 in execution_table). It tells Ruby that the module definition is complete.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 3?
AThe module is closed
BThe method returns the string 'Hello!'
CThe method say_hello is defined
DThe module Greetings is created
💡 Hint
Check the 'Action' and 'Result' columns at step 3 in the execution_table.
At which step is the module declaration completed?
AStep 2
BStep 4
CStep 5
DStep 3
💡 Hint
Look for the 'end' keyword that closes the module in the execution_table.
If we remove 'self.' from the method definition, what changes in variable_tracker?
AThe method will not be added to the module object directly
BThe module will have the method as a module method
CThe module will not be created
DThe method will return nil
💡 Hint
Refer to the key_moments explanation about 'self.' usage and the variable_tracker row for Greetings.
Concept Snapshot
Ruby module declaration syntax:
module ModuleName
  def self.method_name
    # code
  end
end
- Use 'module' to start and 'end' to finish
- 'self.' makes methods callable on the module itself
- Modules group related methods/constants
Full Transcript
This visual execution trace shows how to declare a module in Ruby. First, the module is started with the 'module' keyword and a name. Inside, methods can be defined, often with 'self.' to make them module methods callable directly on the module. The method say_hello returns a string. The module declaration ends with 'end'. Variables track the module object creation and method addition. Key moments clarify why 'self.' is used and the role of 'end'. The quiz tests understanding of each step and the effect of removing 'self.'.

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

  1. Step 1: Recognize Ruby module syntax

    In Ruby, modules are declared using the keyword module followed by the module name and end.
  2. Step 2: Identify the correct declaration

    module MathTools # code here end uses module MathTools ... end, which is the correct syntax for a module.
  3. Final Answer:

    module MathTools # code here end -> Option B
  4. 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

  1. Step 1: Check Ruby module block delimiters

    Ruby modules use module ... end without braces { }.
  2. Step 2: Identify invalid syntax

    module Tools { def help end } uses braces { } which is invalid syntax for module declaration in Ruby.
  3. Final Answer:

    module Tools { def help end } -> Option A
  4. 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

  1. 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.
  2. Step 2: Evaluate the method call output

    Calling Greetings.say_hello returns the string "Hello!", which is printed by puts.
  3. Final Answer:

    Hello! -> Option A
  4. 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

  1. Step 1: Check method definition inside module

    The method help is defined as an instance method, but called on the module directly.
  2. Step 2: Understand method call context

    To call Tools.help, help must be a module method defined with self.help.
  3. Final Answer:

    Method 'help' should be defined with 'self.' to call on module -> Option C
  4. 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

  1. Step 1: Understand nested module syntax

    In Ruby, nested modules can be declared by placing one module inside another.
  2. Step 2: Identify correct nested declaration

    module Outer module Inner end end correctly nests module Inner inside module Outer using indentation and end keywords.
  3. Final Answer:

    module Outer module Inner end end -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard