-----------------------------------------------------
module Dummy
def TryMe
puts "yo"
end
end
-----------------------------------------------------
Now, I try and run this program, called 'testdummy.rb':
-----------------------------------------------------
#!/usr/bin/env ruby
require 'dummy'
include Dummy
TryMe()
def main
TryMe()
end
main()
-----------------------------------------------------
I get, as expected:
----------
yo
----------
Now, I try and run this program, called 'testdummy2.rb':
-----------------------------------------------------
#!/usr/bin/env ruby
require 'dummy'
Dummy::TryMe()
def main
Dummy::TryMe()
end
main()
----------------------------------------------------
I get:
-----------------
testdummy2.rb:3: undefined method `TryMe' for Dummy:Module (NoMethodError)
-----------------
Now, if I go ahead and change the 'dummy.rb' module to look like this:
----------------------------------------------------
module Dummy
def self.TryMe
puts "yo"
end
end
----------------------------------------------------
testdummy2.rb gives me:
------------------------
yo
------------------------
and testdummy.rb gives me:
------------------------
testdummy.rb:4: undefined method `TryMe' for main:Object (NoMethodError)
------------------------
QUESTION: How can I create module 'Dummy' so that it can be called
either with the syntax in 'testdummy.rb' or 'testdummy2.rb' without
modification?
module Dummy
def try_me
puts "yo"
end
extend self
end
include Dummy
try_me
Dummy::try_me
__END__
You just add "extend self", as above. Also it isn't a good idea to
name methods in CamelCase, as that is normally used for constants like
classes, etc. Which is why I used try_me for the method name.
Ryan
···
On 10/21/05, Glenn M. Lewis <noSpam@nospam.com> wrote:
QUESTION: How can I create module 'Dummy' so that it can be called
either with the syntax in 'testdummy.rb' or 'testdummy2.rb' without
modification?
Now, I try and run this program, called 'testdummy.rb':
-----------------------------------------------------
#!/usr/bin/env ruby
require 'dummy'
include Dummy
TryMe()
def main
TryMe()
end
main()
[...]
Now, I try and run this program, called 'testdummy2.rb':
-----------------------------------------------------
#!/usr/bin/env ruby
require 'dummy'
Dummy::TryMe()
def main
Dummy::TryMe()
end
main()
----------------------------------------------------
[...]
QUESTION: How can I create module 'Dummy' so that it can be called
either with the syntax in 'testdummy.rb' or 'testdummy2.rb' without
modification?
Option 1)
module Dummy
def TryMe
puts "yo"
end
# Add all instance methods of the Dummy module to
# the Dummy module object (here referenced as "self").
extend self
end
Option 2)
module Dummy
def TryMe
puts "yo"
end
# give all method names (as symbols) as argument
# to module_function
module_function :TryMe, :SomeOtherMethod
end
HTH,
Stefan
···
On Friday 21 October 2005 21:06, Glenn M. Lewis wrote:
Me too, I was just mimicking the OP's code (with the exclusion of the
CamelCase method name, as noted.) The double colon is much harder to
type than the period.
Ryan
···
On 10/21/05, David A. Black <dblack@wobblini.net> wrote:
Or:
Dummy.try_me
(I admit I prefer the consistent use of '.' for all method calls,
regardless of what the receiver is.)