Reputation: 2152
I know how a module can be used in a class in Ruby:
module Calculator
def add(a,b)
a+b
end
end
class Watch
include Calculator
def time
Time.now
end
end
w = Watch.new()
puts w.time # 2020-03-11 22:34:01 +0000
puts w.add(3,5) # 8
But, sometimes I can see some module has a class inside. For instance, in Rails, helpers:
module MyModule
class MyClass
def foo
puts 'foo'
end
end
end
Why would I have a class inside a module?
Upvotes: 0
Views: 62
Reputation: 148
Modules can be used that way for scoping purposes. In your example you can refer to MyClass
from outside the module by MyModule::MyClass
.
There are several uses for this, like grouping together related classes under a common namespace.
It's better to get more information about this from some Ruby tutorials (for examples and stuff). Check out "Ruby - Modules and Mixins" for more information.
Upvotes: 3