I have encountered following situation:
There is
ModuleA::ModuleB::ClassC.do_something
in the definition of do_something I need to use model from the application
def do_something
...
data = Order.all
...
end
But there also exists a module
ModuleA::Order
So I get an error
undefined method `all' for ModuleA::Order:Module
I found a solution by doing
def do_something
...
data = Kernel.const_get('Order').all
...
end
That returns the model. My question is: what's the best way to do it? is there a cleaner solution? (despite the fact, that having the same name for Class and Module it's not the greatest idea, but it cannot be changed here...)
Class and Module can not be of same name (Example)
This is not possible with the pip. All of the packages on PyPI have unique names. Packages often require and depend on each other, and assume the name will not change. Even if you manage to put the code on Python path, when importing a module, python searches the paths in sys.
What is the difference between a class and a module? Modules are collections of methods and constants. They cannot generate instances. Classes may generate instances (objects), and have per-instance state (instance variables).
Python classes store the names of their methods within the internal dictionary known as . __dict__ that holds class namespace. Similar to the other Python dictionary . __dict__ can't contain repeated keys, which means we cannot have more than one method with the same name within the same class.
Prefix the class name with ::
in the do_something
method...
def do_something
...
data = ::Order.all
...
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With