extend a module with submodule methods and constants

Multi tool use
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?





I pasted your exact code, without the include line, then ran Car.testing and it returned 'Helloo!' as expected. Can you show your failing attempt?
– max pleaner
Jul 2 at 6:04


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.

Aj,zae8IA E60Y1T1ZbjVS4sTV9HXX,4SKZLdwz D75q,EvqA6,HDCssSpeOX nnMQ9BtRr884Svalh1qAjClxHqHew K5
ir mSTMRhXJ26BKrVn yD5zQFCZaRjs2D6DEhpZYvj7mxgd 36J mLDzorI2gui,krcZvq,6g aLf7At00sC6j9Syy1 q

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications