Ok i have some very basic question, and i'm not even sure if this is requred to do, but i would like to ask how to call a method from a class that's inside a module.
module Testing class Networking def self.method5() msgbox_p("called") end endend networking = Networking.newTesting::networkingnetworking.method5()Obviously the last 3 lines are wrong.
So i have 3 questions
Is it possible to run only the "method5"
Is it possible to run the whole class, executing all the methods inside it?
Is it possible to run the whole module , executing all the classes along with all the methods inside?
Calling a method inside a class inside a module
● ARCHIVED · READ-ONLY
-
-
Do note that Ruby is case-sensitive.
The answer to all your questions are Yes.
For the first question, you can choose whether you want parenthesis or not. It's a matter of preference, so choose what you want. The default scripts follows the style of only using parenthesis when you pass arguments on to a method.
Testing::Networking.method5Testing::Networking.method5()
I do not think that you really want to execute all methods the class contains. You see, there are quite a few.
networkClass = Testing::Networkingp *networkClass.methodsMaybe something like only focusing on non-inherited methods could be the case
p *(networkClass.methods - networkClass.superclass.methods)
The classes you create within the module declaration are assigned as constants to the module. So you can access the constants through there.
p *Testing.constants # For exampleTesting.constants.each do |const_symbol| constant = Testing.const_get(const_symbol) # Do stuffend
The constant retrieve could be pretty much anything. You can try to look at its ancestry, or follow the duck-typing pattern. I hope this should be enough to get you started.
To make matters more fun you should read up on Singleton classes in ruby. How they affect my examples, I do not know.
*hugs* -
Thank you soo much
I thought you had to write
Testing::Networking.new.method5()
so i guess that why it didn't work when i tried it.
For the second one; i didn't know it would try to run all the hidden methods.
Is there actually a specific name for all these, what i presume are, hidden prewritten methods that are already in the Ruby language?
So that i could google specific ones to get some explanations.
And for the third one, i would like to ask what these symbols | | do around const_symbol
I know you can use them as a union between arratys or as an "or" funcion if you use ||
but i can't find any explained examples where you use it like a bracket.
(Excuse the terminology, i'm still not completely familliar with it)