Quick Answer
Preventing naming collisions with modules.
Understanding the Issue
Modules can encapsulate classes and methods to avoid naming conflicts. They also enable mixin functionality.
The Problem
This code demonstrates the issue:
Ruby
Error
# Problem: Naming conflict
class Calculator; end
# Another library also has Calculator
The Solution
Here's the corrected code:
Ruby
Fixed
# Solution: Module namespace
module Finance
class Calculator
def self.interest(amount, rate)
amount * rate
end
end
end
Finance::Calculator.interest(1000, 0.05)
Key Takeaways
Use modules to organize code and prevent collisions.