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

时间:2022-11-15 20:49:35

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. It's not a class method obviously, so I can't run it like:

我只想运行mymethod。显然它不是类方法,所以我不能这样运行:

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. It should be easier than that, shouldn't it?

但是我希望有一种方法可以让解析器对方法进行评估,而不需要经过很多module_eval和module_function之类的东西。应该比这更容易,不是吗?

2 个解决方案

#1


27  

To run it from the rails console you just have to include it:

要从rails控制台运行它,您只需包含它:

> include Reports::MyMod
> mymethod

#2


1  

class A
  include Reports::MyMod
end

A.new.mymethod

#1


27  

To run it from the rails console you just have to include it:

要从rails控制台运行它,您只需包含它:

> include Reports::MyMod
> mymethod

#2


1  

class A
  include Reports::MyMod
end

A.new.mymethod