Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6225823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:54:54+00:00 2026-05-24T08:54:54+00:00

I would like to put some code in module that throws an error if

  • 0

I would like to put some code in module that throws an error if certain method is not defined.
This module relies on the external definition of this method, since this method’s implementation is different for all classes. This code would help developers know early that they forgot to implement the method rather than when they tried to use features of the module.

module MyModule
  def self.included(klass)
    raise "MyModule: please `def my_method` on #{klass}" unless klass.respond_to?(:my_method)
  end
end 

I can easily raise an error in a module’s included definition if a method is not defined, however since most modules are included at the top of a file, it’s likely that my required method is defined in the class, but not before my module is included.

class MyClass
  include MyModule
  def self.my_method
    # ...
  end
end

This would still raise an error 🙁

Is it possible to raise an error only if the method truly is not defined in the class definition? Almost need a class.onload callback of sorts. If not, any other ideas for how to mitigate the possibilities that a programmer might include our module without defining this needed method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:54:54+00:00Added an answer on May 24, 2026 at 8:54 am

    Sounds like you want to make use of method_missing and define_method.

    If you do use method_missing don’t forget to:

    • call super for unhandled cases.
    • also implement a respond_to? method

    look at this question, plus this and that.

    Update:

    It sounds the goal is to do static method checking like Java or c++ does. This is not really meaningful in ruby 🙁

    Since in ruby:

    • Each instance of an object has its own eigenclass. A given object may have the necessary methods mixed in at runtime. So just because Foo does not have a method at class load time is meaningless.
    • Frameworks like RoR hooks method_missing and dynamically create methods needed for the database query methods, so the method may exist (or not) when it is needed.

    With regards to “class on load”: A class definition is really executed. Try this:

    class Foo 
      p "Hi"
    end
    

    You will see “Hi” the first and only the first time Foo is used. This is how things like devise hook into do their magic.

    class User < ActiveRecord::Base
      # **CALL 'devise' method**
      devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
    
      # **CALL attr_accessible method**
      attr_accessible :email, :password, :password_confirmation
    end
    

    So maybe by private convention have developers add a check_class method call to the bottom of the classes in question?

    I understand the intent but it seems like fighting the way ruby is designed to work.

    As a mostly Java person I appreciate the frustration. Let me guess: repeated cases of code getting pushed to production that had missing methods? 😛

    Update2:

    wrt onload In ruby barring use of frozen a class get new methods defined all the time. ( Or an instance can get new methods defined just for that instance. ) so checking for a method’s nonexistence is only a snapshot check and not as definitive a check as a static language brings to the table. This is ruby’s very own Halting problem

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long method, and for reading clarity would like to put some
I would like to put a string into a byte array, but the string
I would like to put a link to a webpage in an alert dialog
I would like to know how to put all of my ListView rows into
I have a Window where I have put a Frame. I would like to
Using sql server 2000, I would like to take my production data and put
I've implemented some extension methods as defined here . I've put these into a
this is a continuation of the discussion I started here . I would like
I would like to click a div on a page to run a certain
Would like to get a list of advantages and disadvantages of using Stored Procedures.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.