extend a module with submodule methods and constants

Multi tool use
extend a module with submodule methods and constants
I want to be able to extend a module with the methods and constants of a submodule.
If I use extend
I receive undefined constant.
extend
The only way I made it work is using both extend
and include
. I tried also using self.method on submodule.
extend
include
module Car
module Container
HOLA = 'Helloo!'
def testing
HOLA
end
end
include Container
extend Container
end
So this both should work:
Car.testing # Hello!
Car::HOLA # Hello!
I guess this is a code smell..., but what other ways to make it work you know?
include
Car.testing
'Helloo!'
Yes,
Car.testing
works but Car::HOLA
does not.– Arnold Roa
Jul 3 at 3:17
Car.testing
Car::HOLA
1 Answer
1
As per the description shared, it seems like you want to access the nested module constants and methods in some_other_class.
This is the module definition as mentioned in the post.
module Car
module Container
HOLA = 'Helloo!'
def testing
HOLA
end
end
end
Now Suppose , that you want to use this method in some class say Vehicle
require 'car' #this is the module file since it is residing in some other file
class Vehicle
extend Car
def test
Car.testing
end
end
Now calling Vehicle.new.test will print
"Helloo!"
Hope it helps!!
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I pasted your exact code, without the
include
line, then ranCar.testing
and it returned'Helloo!'
as expected. Can you show your failing attempt?– max pleaner
Jul 2 at 6:04