Skip to content

Instantly share code, notes, and snippets.

@reconbot
Created October 1, 2012 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reconbot/3811831 to your computer and use it in GitHub Desktop.
Save reconbot/3811831 to your computer and use it in GitHub Desktop.
Module Methods via Reflection
module Routes
def home
#stuff
end
def work
#end
end
end
Routes.method_defined? :home
# true
Routes.send(:home)
#NoMethodError: undefined method `home' for Routes:Module
@koudelka
Copy link

koudelka commented Oct 1, 2012

method_defined? tells you if the module defines the instance method (for mixing in): http://www.ruby-doc.org/core-1.9.3/Module.html#method-i-method_defined-3F

maybe you wanted:

module Routes
  def self.home
    'hello thar'
  end

  def self.work
    #end
  end
end

Routes.method_defined? :home
# false

Routes.methods.include?(:home)
# true

Routes.send(:home)
# "hello thar"

@reconbot
Copy link
Author

reconbot commented Oct 1, 2012

I did want that, I ended up using Routes.respond_to? instead of the include? and now I know the difference between the module methods and instance methods in modules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment