Bird
Raised Fist0
Rubyprogramming~20 mins

Module methods in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Module Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of calling a module method directly
What is the output of this Ruby code?
Ruby
module Greetings
  def self.say_hello
    "Hello from module!"
  end
end

puts Greetings.say_hello
AHello from module!
BNoMethodError
Cnil
DSyntaxError
Attempts:
2 left
💡 Hint
Look at how the method is defined with self. It means it belongs to the module itself.
❓ Predict Output
intermediate
2:00remaining
Output of including a module with a module method
What happens when you include a module with a module method and call that method on the including class?
Ruby
module Tools
  def self.info
    "Tools module info"
  end
end

class Machine
  include Tools
end

puts Machine.info
ATools module info
BNoMethodError
Cnil
DSyntaxError
Attempts:
2 left
💡 Hint
Including a module adds instance methods, but module methods are not included as class methods.
🔧 Debug
advanced
3:00remaining
Fix the code to call a module method from a class
This code tries to call a module method from a class method but raises an error. Which option fixes it?
Ruby
module MathHelpers
  def self.square(x)
    x * x
  end
end

class Calculator
  def self.square(x)
    square(x)
  end
end

puts Calculator.square(4)
AChange MathHelpers.square to def square(x) without self
BAdd include MathHelpers inside Calculator
CChange Calculator.square to call MathHelpers.square(x)
DChange Calculator.square to an instance method
Attempts:
2 left
💡 Hint
Module methods with self need to be called with the module name.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in module method definition
Which option shows the correct way to define a module method in Ruby?
A
module M
  def method:
    puts 'Hi'
  end
end
B
module M
  def method(self)
    puts 'Hi'
  end
end
C
module M
  def method.self
    puts 'Hi'
  end
end
D
module M
  def self.method
    puts 'Hi'
  end
end
Attempts:
2 left
💡 Hint
Module methods use self. before the method name.
🚀 Application
expert
3:00remaining
How many methods does this module add to a class?
Given this module and class, how many methods does the class have after including the module?
Ruby
module Features
  def self.module_method
    'module method'
  end

  def instance_method
    'instance method'
  end
end

class Product
  include Features
end

methods = Product.methods - Object.methods
instance_methods = Product.instance_methods(false)
Amethods: 0, instance_methods: 1
Bmethods: 1, instance_methods: 1
Cmethods: 1, instance_methods: 0
Dmethods: 0, instance_methods: 0
Attempts:
2 left
💡 Hint
Including a module adds instance methods, but not module methods as class methods.

Practice

(1/5)
1. What is the correct way to define a method inside a Ruby module so it can be called directly on the module?
easy
A. Define the method outside the module
B. Define the method without self inside the module
C. Define the method with self.method_name inside the module
D. Define the method with def module.method_name inside the module

Solution

  1. Step 1: Understand module method definition

    In Ruby, to make a method callable directly on a module, you define it with self.method_name inside the module.
  2. Step 2: Compare options

    Define the method with self.method_name inside the module uses self.method_name, which is correct. Other options either omit self or use invalid syntax.
  3. Final Answer:

    Define the method with self.method_name inside the module -> Option C
  4. Quick Check:

    Module methods use self.method_name = A [OK]
Hint: Use self.method_name to define module methods [OK]
Common Mistakes:
  • Forgetting to add self before method name
  • Defining methods outside the module
  • Using invalid syntax like def module.method_name
2. Which of the following is the correct syntax to call a module method named greet defined in module Welcome?
easy
A. Welcome#greet
B. greet.Welcome
C. Welcome.new.greet
D. Welcome.greet

Solution

  1. Step 1: Recall how to call module methods

    Module methods are called using dot notation: ModuleName.method_name.
  2. Step 2: Check options

    Welcome.greet is the correct syntax using dot notation. Welcome#greet incorrectly uses #. greet.Welcome reverses the order. Welcome.new.greet confuses it with an instance method call.
  3. Final Answer:

    Welcome.greet -> Option D
  4. Quick Check:

    Call module methods with dot = D [OK]
Hint: Call module methods with ModuleName.method_name [OK]
Common Mistakes:
  • Using # instead of . to call module methods
  • Trying to call method with invalid syntax
  • Confusing instance and module method calls
3. What will be the output of the following Ruby code?
module MathTools
  def self.square(x)
    x * x
  end
end

puts MathTools.square(4)
medium
A. 16
B. square
C. Error: undefined method
D. 4

Solution

  1. Step 1: Understand the method definition

    The method square is defined as a module method using self.square. It returns the square of the input.
  2. Step 2: Calculate the output

    Calling MathTools.square(4) returns 4 * 4 = 16, which is printed.
  3. Final Answer:

    16 -> Option A
  4. Quick Check:

    4 squared = 16 [OK]
Hint: Multiply input by itself in module method [OK]
Common Mistakes:
  • Forgetting self and causing undefined method error
  • Confusing output with input
  • Expecting method name as output
4. Identify the error in this Ruby module method code:
module Greetings
  def greet
    "Hello!"
  end
end

puts Greetings.greet
medium
A. Method greet is not defined as a module method
B. Module name is incorrect
C. Missing parentheses in method call
D. Syntax error in method body

Solution

  1. Step 1: Check method definition inside module

    The method greet is defined without self., so it is an instance method, not a module method.
  2. Step 2: Understand method call

    Calling Greetings.greet tries to call a module method, but none exists, causing an error.
  3. Final Answer:

    Method greet is not defined as a module method -> Option A
  4. Quick Check:

    Missing self. means no module method [OK]
Hint: Add self. to define module methods [OK]
Common Mistakes:
  • Defining instance methods instead of module methods
  • Calling module method without defining it
  • Assuming parentheses are required in method call
5. Given the module below, how can you add a module method double that returns twice the input number?
module Calculator
  # Add your method here
end
hard
A. def double(x) x + x end
B. def self.double(x) x * 2 end
C. def Calculator.double(x) x * 2 end
D. def self.double x * 2 end

Solution

  1. Step 1: Define a module method with self

    To create a module method, define it with self.method_name inside the module.
  2. Step 2: Write method to return twice the input

    The method should take one argument x and return x * 2.
  3. Final Answer:

    def self.double(x) x * 2 end -> Option B
  4. Quick Check:

    Module method with self and correct logic = A [OK]
Hint: Use def self.method(args) for module methods [OK]
Common Mistakes:
  • Omitting self and creating instance method
  • Missing method argument
  • Defining method outside module
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