Run a method in a Module

Hi,
I have two different modules that contain a few methods with the same
name so I cannot include the modules in the code and I need to do
something like:

module Module1
def myMethod
puts “hi”
end
def others
end
end

module Module2
def myMethod
puts “more hi”
end
end

a=MyModule1.myMethod
b=MyModule2.myMethod

But it’s not working, the error is: undefined method ‘myMethod’

any idea??

Thanks.

In my case is a little bit more complicated since the modules I’ll use
are the output of another application and I cannot change the content.
I’m doing something like:

m1=Module1
m1.instance_methods.each {|b|

here I’m checking if the methods exist on Module2

Also I’m running the method and getting the result in both modules

}

On Wed, Nov 19, 2008 at 11:40 AM, Mario R. [email protected] wrote:

end

But it’s not working, the error is: undefined method ‘myMethod’

any idea??

If you want to call a method from a Module that’s not supposed to be
part of the instance of a class, then you can try this:

module Module1
def self.myMethod
puts “hi”
end
def others
end
end

irb(main):020:0> Module1.myMethod
hi

Jesus.

So, there is not an easier way to call a method inside a module…
something like:

MyModule.myMethod

or

MyModule.method(myName).call

thank you.

So, there is not an easier way to call a method inside a module…
something like:

MyModule.myMethod

or

MyModule.method(myName).call

How about this:
irb(main):001:0> module MyModule
irb(main):002:1> class MyClass
irb(main):003:2> def mymethod
irb(main):004:3> “hello”
irb(main):005:3> end
irb(main):006:2> end
irb(main):007:1> end
=> nil
irb(main):008:0> MyModule::MyClass.new.mymethod
=> “hello”

saji


Saji N. Hameed

APEC Climate Center +82 51 668 7470
National Pension Corporation Busan Building 12F
Yeonsan 2-dong, Yeonje-gu, BUSAN 611705 [email protected]
KOREA

On Wed, Nov 19, 2008 at 12:19 PM, Mario R. [email protected] wrote:

In my case is a little bit more complicated since the modules I’ll use
are the output of another application and I cannot change the content.
I’m doing something like:

m1=Module1
m1.instance_methods.each {|b|

here I’m checking if the methods exist on Module2

Also I’m running the method and getting the result in both modules

}

One way I managed to do it, although I’m not sure it makes sense for
your case is:

irb(main):039:0> module Test
irb(main):040:1> def met
irb(main):041:2> puts “hi”
irb(main):042:2> end
irb(main):043:1> end
=> nil
irb(main):053:0> o = Object.new
irb(main):054:0> o.extend(Test)
=> #Object:0xb7b543a4
irb(main):057:0> Test.instance_method(“met”).bind(o).call
hi

That’s because to call an instance method you need an object that is
the instance. So we build a “fake” object, extend it with the module,
bind the method to it and call it. Or you could just call the method
directly on the object:

irb(main):059:0> o.send(“met”)
hi

So for your case:

irb(main):060:0> o = Object.new
=> #Object:0xb7c2c0ec
irb(main):061:0> o.extend(Test)
=> #Object:0xb7c2c0ec
irb(main):062:0> Test.instance_methods.each {|m| o.send(m)}
hi

Of course this assumes (and I think that your use case too) that the
instance methods of the Module don’t expect anything in the instance
binding they are called: i.e. they don’t expect an ‘each’ method
defined or something like that.

Jesus.