Reputation: 1601
I am familiar with the concept of nesting classes and modules within another module and grouping them in a namespace. What is the idea / purpose behind
Nesting classes within another class
class A
class B
def method_B
...
end
end
end
Nesting modules within another class
class A
module c
def method_c
...
end
end
end
thanks, ash
Upvotes: 3
Views: 3871
Reputation: 237010
Classes are also namespaces, so it's the same idea. Class is a subclass of Module, so if you get it in the context of modules, you also get it in the context of classes.
Upvotes: 3
Reputation: 14185
It is all about grouping related concerns while exposing sensible semantics. As an example of number 1 an HTTP::Request (Request class embedded in a larger HTTP protocol class) is a quite different thing from an FTP::Request. With modules it enables the common ruby idiom of Behavior::InstanceMethods and Behavior::ClassMethods for handling mixins.
Upvotes: 1