user3675188
user3675188

Reputation: 7409

How could I access global method from class method both are in the same module

I got this error

NoMethodError (undefined method `get_routes' for

How could I access get_routes from 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  

Upvotes: 1

Views: 22

Answers (1)

Kumar Akarsh
Kumar Akarsh

Reputation: 4980

You need to include the module FlightUtil in the Sample class.

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  

A Module is a collection of methods and constants. Classes nested inside of modules are used to namespace the classes. You have to include a module in order to access the module's contents (methods or constants).

Upvotes: 1

Related Questions