简体   繁体   English

如何从Rails控制台执行模块中的方法?

[英]How can I execute a method in a module from the Rails console?

I have a module in a file called my_mod.rb declared like this: 我在一个名为my_mod.rb的文件中有一个模块,声明如下:

module Reports
  module MyMod

    def mymethod
      ...
    end

  end
end

I just want to run mymethod . 我只是想运行mymethod It's not a class method obviously, so I can't run it like: 它显然不是一个类方法,所以我不能运行它:

Reports::MyMod.mymethod

and yet I was hoping there was some way to get the method evaluated by the parser without have to go through a bunch of module_eval and module_function stuff. 然而我希望有一些方法可以让解析器评估该方法,而无需经过一堆module_eval和module_function的东西。 It should be easier than that, shouldn't it? 它应该比那更容易,不应该吗?

To run it from the rails console you just have to include it: 要从rails控制台运行它,您只需要包含它:

> include Reports::MyMod
> mymethod
class A
  include Reports::MyMod
end

A.new.mymethod

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM