module_function question

M

Martin Pirker

Hi...

Consider...

-----------------------------------------------
module Mymodule
def call_test1
printf "test1"
end
module_function :call_test1

def call_test2
printf "test2"
end
module_function :call_test2

def functions
h = Hash.new
Mymodule.methods.grep(/^call/).each do |f|
h[f]=Mymodule.method(f)
end
h
end
module_function :functions
end

calls = Mymodule::functions
calls.keys.each do |thisone|
calls[thisone].call
puts " #{thisone}"
end
-----------------------------------------------

Q: How to optimize/automate the lines

module_function :call_test1
module_function :call_test2

away, too? (since we know all call_* are the same type and want to
remove that hand typed error source)


tnx!
Martin
 
R

Robert Klemme

Martin Pirker said:
Hi...
away, too? (since we know all call_* are the same type and want to
remove that hand typed error source)

Variant 1

module Mymodule
def self.call_test1
printf "test1"
end
# module_function :call_test1

def self.call_test2
printf "test2"
end
# module_function :call_test2

def self.functions
h = Hash.new
methods.grep(/^call/).each do |f|
h[f]=method(f)
end
h
end
end

Mymodule.functions.each do |name, fun|
fun.call
puts " #{name}"
end


Variant 2

module Mymodule
class << self

def call_test1
printf "test1"
end

def call_test2
printf "test2"
end

def functions
h = Hash.new
methods.grep(/^call/).each do |f|
h[f]=method(f)
end
h
end

end
end

Mymodule.functions.each do |name, fun|
fun.call
puts " #{name}"
end


Variant 3

module Mymodule
module_function

def call_test1
printf "test1"
end

def call_test2
printf "test2"
end

def functions
h = Hash.new
methods.grep(/^call/).each do |f|
h[f]=method(f)
end
h
end
end

Mymodule.functions.each do |name, fun|
fun.call
puts " #{name}"
end


Variant 4

module Mymodule
module_function

def call_test1
printf "test1"
end

def call_test2
printf "test2"
end

def functions
h = Hash.new
methods.each do |f|
h[f]=method(f) if /^call/ =~ f
end
h
end
end

Mymodule.functions.each do |name, fun|
fun.call
puts " #{name}"
end


Do you need more?

robert
 
M

Martin Pirker

Robert Klemme said:
Variant 1 [...]
Variant 2 [...]
Variant 3 [...]
Variant 4 [...]

Do you need more?

:) No, I opt for the self. way

thanks for saving ~200 lines :)


Martin
 
T

ts

R> Variant 1
R> Variant 2

don't work : call_test1 and call_test2 are not module functions

R> Variant 3
R> Variant 4

all methods after #module_function are module_functions (not only call_),
perhaps this is not what he want

R> Do you need more?

yes :)


Guy Decoux
 
R

Robert Klemme

ts said:
R> Variant 1
R> Variant 2

don't work : call_test1 and call_test2 are not module functions

I know that these do not exactly the same as module_function but Martin
seemed to need only the module instance singleton methods and not instance
methods.
R> Variant 3
R> Variant 4

all methods after #module_function are module_functions (not only call_),
perhaps this is not what he want

I think he wanted a fast and error safe way to define a set of singleton
methods for a module instance. That's what I extract from his postings -
but I could be wrong of course.
R> Do you need more?

yes :)

I could come up with some permutations... :))

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,084
Latest member
HansGeorgi

Latest Threads

Top