Run a method in a Module

M

Mario Ruiz

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.
 
J

Jesús Gabriel y Galán

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??

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.
 
M

Mario Ruiz

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
}
 
J

Jesús Gabriel y Galán

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.
 
M

Mario Ruiz

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.
 
S

Saji N. Hameed

* Mario Ruiz said:
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 (e-mail address removed)
KOREA
 

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

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top