Module_function question

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

"Martin Pirker" <crf@sbox.tu-graz.ac.at> schrieb im Newsbeitrag news:413f37ad$0$9314$3b214f66@aconews.univie.ac.at...

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

Variant 1

[...]

Variant 2

[...]

Variant 3

[...]

Variant 4

[...]

Do you need more?

:slight_smile: No, I opt for the self. way

thanks for saving ~200 lines :slight_smile:

Martin

···

Robert Klemme <bob.news@gmx.net> wrote:

Variant 1
Variant 2

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

Variant 3
Variant 4

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

Do you need more?

yes :slight_smile:

Guy Decoux

"ts" <decoux@moulon.inra.fr> schrieb im Newsbeitrag
news:200409090935.i899ZbC14899@moulon.inra.fr...

> Variant 1
> 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.

> Variant 3
> 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.

> Do you need more?

yes :slight_smile:

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

Kind regards

    robert