从其他类调用模块方法(Call module method from other class)

编程入门 行业动态 更新时间:2024-10-11 13:20:56
从其他类调用模块方法(Call module method from other class)

所以我有一个ruby文件驻留在model> service中

module Services module SomeJobs def mainJob ... end end end

以及如何从位于lib / testfunction.rb中的ruby类调用该方法

我尝试了以下内容,但没有奏效。 任何帮助是欣赏。 我正在尝试调试代码。

class TestFunction include SomeJobs TestFunction::mainJob end

So i have one ruby file that is reside inside model > service

module Services module SomeJobs def mainJob ... end end end

and how do i call that method from ruby class that sit inside lib/testfunction.rb

I tried the following and it did not work. any help is appreciate. I am trying to debug the code.

class TestFunction include SomeJobs TestFunction::mainJob end

最满意答案

试试这个

module Services module SomeJobs def self.mainJob end end end

make mainJob是一个模块方法,因为模块实例方法永远不会包含在包含类中,它们对模块是私有的

class TestFunction include Services::SomeJobs end

现在打来电话

在这个TestFunction类之外

TestFunction.new.mainJob

并且在这个TestFunction类中

self.class.new.mainJob

如果你想以类方法的形式访问mainJob,那么使用extend而不是include。

因为您正在使用IDE调试器

尝试要求相对于rails应用程序的文件,在你的

TestFunction类

try this out

module Services module SomeJobs def self.mainJob end end end

make mainJob a module method, as module instance method are never included in the including class, they are private to module

class TestFunction include Services::SomeJobs end

now call from

outside this TestFunction class like

TestFunction.new.mainJob

and inside this TestFunction class with

self.class.new.mainJob

if you want to access mainJob as class method then, use extend instead of include.

as you are using IDE debugger

try requiring that file relative to rails application, in your

TestFunction Class

更多推荐

本文发布于:2023-07-17 14:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1145684.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   方法   Call   class   method

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!