0
0
Rubyprogramming~15 mins

Module declaration syntax in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Module declaration syntax
📖 Scenario: You are organizing a small Ruby program that groups related methods inside a module. This helps keep your code neat and reusable.
🎯 Goal: Create a Ruby module named MathHelpers that contains a method to add two numbers.
📋 What You'll Learn
Create a module named MathHelpers
Inside the module, define a method called add that takes two parameters a and b
The add method should return the sum of a and b
Call the add method from the MathHelpers module with arguments 5 and 7
Print the result of the method call
💡 Why This Matters
🌍 Real World
Modules are used in Ruby to group related methods and constants, making code easier to maintain and reuse.
💼 Career
Understanding modules is important for Ruby developers to organize code and use libraries effectively.
Progress0 / 4 steps
1
Create the module declaration
Write a module declaration named MathHelpers.
Ruby
Need a hint?

Use the module keyword followed by the module name and end to close it.

2
Define the add method inside the module
Inside the MathHelpers module, define a method called add that takes two parameters a and b.
Ruby
Need a hint?

Use def self.add(a, b) to define a module method that can be called without creating an object.

3
Call the add method from the module
Call the add method from the MathHelpers module with arguments 5 and 7, and store the result in a variable called result.
Ruby
Need a hint?

Use MathHelpers.add(5, 7) to call the method and assign it to result.

4
Print the result
Print the value of the variable result.
Ruby
Need a hint?

Use puts result to display the sum.