我怎么能从类方法访问全局方法都在同一个模块中

时间:2015-07-18 22:48:28

标签: ruby

我收到了这个错误

  

NoMethodError(

的未定义方法`get_routes'

如何从get_routes

访问Sample.run
module FlightUtil
  extend ActiveSupport::Concern

  def get_routes(from="TAIPEI", to="OSAKA")
    ~~~
  end

  class Sample
    def run
     get_routes("A", "B")
    end
  end


end  

1 个答案:

答案 0 :(得分:1)

您需要在Sample类中包含模块FlightUtil。

module FlightUtil
  extend ActiveSupport::Concern

  def get_routes(from="TAIPEI", to="OSAKA")
    ~~~
  end

  class Sample
    include FlightUtil
    def run
     get_routes("A", "B")
    end
  end


end  

模块是方法和常量的集合。嵌套在模块内的类用于命名类。您必须包含一个模块才能访问模块的内容(方法或常量)。