Reputation: 48045
This is my code:
module RubyEditExtComponent
def eventExt
watchExt "banana"
end
end
class RubyEditExt
def self.watchExt(value)
puts value
end
include RubyEditExtComponent
end
RubyEditExt.new.eventExt
I want to call a specific (parent) class method that will output the value I'll pass to it. But it says undefined method watchExt
.
Where am I wrong?
Upvotes: 0
Views: 79
Reputation: 3320
May be, what you are trying to do is to extend your class RubyEditExt
with functionality from the module RubyEditExtComponent
. This has nothing to do with inheritance (ChildClass < ParentClass). Usually you will do this to modularize functionality, which keeps your classes clean and the modules are reusable. Such modules are called mixins.
See the example below. This way you can extend your class with instance methods which you can call for every object of your class or you define class methods for the class which will include the module.
module RubyEditExtComponent
def self.included(klass)
klass.instance_eval do
include InstanceMethods
extend ClassMethods
end
end
module InstanceMethods
def eventExt
watchExt "banana"
end
end
module ClassMethods
def eventExt2
self.new.watchExt "banana"
end
end
end
class RubyEditExt
include RubyEditExtComponent
def watchExt(value)
puts value
end
end
The method self.included
is called at the inclusion of the module (include RubyEditExtComponent
) and it receives the class which includes it. Now you can call the instance method for an object of your class:
RubyEditExt.new.eventExt
banana
=> nil
Or you call the class method. In this method I create an instance (self.new
) of the class which includes the module (self == RubyEditExt
) and then I call the instance method watchExt
for it.
RubyEditExt.eventExt2
banana
=> nil
Upvotes: 1
Reputation: 121010
watchExt
is called from the instance method, so it either should be an instance method itself:
class RubyEditExt
def watchExt(value) # NO self
puts value
end
...
end
or it should be called as class method:
module RubyEditExtComponent
def eventExt
self.class.watchExt "banana"
end
end
Upvotes: 1