Is it valid and/or acceptable to use a class for namespacing as opposed to a module? Maybe it’s just a matter of opinion, but I thought someone might have experience with this that could say definitively whether or not it’s an accepted practice.
Take the following classes for example:
class Parent
end
class Parent::Child1 < Parent
end
class Parent::Child2 < Parent
end
I know the above code works, but it seems weird to use a class instead of a module for the namespace. The reason I want to add the parent class name to the subclass is because I plan on having a lot of subclasses and want to avoid name conflicts. For example:
class Priority
end
class Priority::Low < Priority
end
class Temperature
end
class Temperature::Low < Priority
end
The alternative to this would be to not use namespaces and just use the parent name as part of the subclass name:
class PriorityLow < Priority
end
Is there a best practice for namespacing in Ruby?
Update:
@sawa asked below for some examples. It’s for a Gem that I built called classy_enum which allows users to define classes that are members of an Enum type. Check out the example usage section for a little more background on it. I am cleaning up the code base and it occurred to me that my suggested naming scheme could be improved. So instead of using PriorityLow, you could use Priority::Low (or whatever makes the most sense, but I’m leaning toward the latter)
I don’t have a definitive answer, but it seems worth noting that the Ruby
Classclass is a subclass ofModule, which means to me that a Class is a type of Module, which suggests that it’s probably reasonable to do what you want. I certainly can’t see any reason to avoid it, though I’m curious to see if anyone comes up with any potential downsides.