Eugen
Eugen

Reputation: 155

Call all methods of a module

I have this module

module Example
  def self.test
    p "test"
  end

  def self.test2
    p "test2"
  end

  def self.test3
    p "test3"
  end
end

Now I have a method which just should call all those methods in the module

def call_module_methods
  #call all example methods
end

So it would call:

Example.test
Example.test2
Example.test3

and the output would be:

"test"
"test2"
"test3"

Is it somehow possible?

Upvotes: 1

Views: 42

Answers (1)

Cristian Lupascu
Cristian Lupascu

Reputation: 40546

Yes, it's possible:

def call_module_methods
  Example.methods(false).each do |m|
    Example.send m
  end
end

The trick is that you need to call methods(false) in order to get only the methods defined directly on the module.

Upvotes: 2

Related Questions