ruby - Why isn't my class method recognized? -
i trying understand why class method not recognized. below code:
wiki_patch.rb
require_dependency 'wiki_content' module wikirecipientpatch def self.included(base) base.send(:include, instancemethods) base.class_eval alias_method_chain :recipients, :send_wiki_mail end end end module instancemethods def self.set_mail_checker(mail) @mail_checker = mail end end rails.configuration.to_prepare wikicontent.send(:include, wikirecipientpatch) end controller.rb
wikicontent.set_mail_checker(params[:mail_checker_wiki]) i getting error:
nomethoderror (undefined method `set_mail_checker' #<class:0x4876560>): any idea why happens , solution is?
you got idiom wrong.
classmethods/instancemethodsmodules supposed nested in "main" module (wikirecipientpatchin case).you including instance methods, expecting class methods somehow arise this? surely meant
extend classmethods, didn't you?module wikirecipientpatch def self.included(base) base.extend classmethods end module classmethods def set_mail_checker(mail) 'mail checker' end end end class wikicontent; end wikicontent.send(:include, wikirecipientpatch) wikicontent.set_mail_checker('whatever') # => "mail checker"
Comments
Post a Comment