Why is is that the following fails?
module Test
def test
puts "test"
end
puts "testing"
test
end
I get this output:
testing
test.rb:6:in `test': wrong number of arguments (ArgumentError)
from test.rb:6:in `<module:Test>'
from test.rb:1:in `<main>'
Is it because the module hasn’t been “compiled” yet, since the end keyword hasn’t been reached?
Using a previously unused name might clear up your confusion:
results in
Inside the
module...endblock, when you invokemy_test, what isself(the implicit receiver)? It is the moduleTest. And there is nomy_test“module method”. Themy_testmethod defined above is like an instance method, to be sent to some object that includes this module.You need to define
my_testas a “module method”:results in
If you want
my_testas an instance method and you want to invoke it inside your module definition:gives