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 5931133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:39:05+00:00 2026-05-22T14:39:05+00:00

Issue: I am using a global constant STORE_DATA_ENABLED to enable a module I wrote

  • 0

Issue:
I am using a global constant STORE_DATA_ENABLED to enable a module I wrote (StoreData):
Example:

STORE_DATA_ENABLED = false

...

module StoreData

    def a
        return unless STORE_DATA_ENABLED
        ...
    end

    def b
        return unless STORE_DATA_ENABLED
        ...
    end

    def c
        return unless STORE_DATA_ENABLED
        ...
    end

    ...

end

I think there is a way to disable the module without checking all the methods in the module.
Any Ideas to DRY this code ?

  • 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-22T14:39:06+00:00Added an answer on May 22, 2026 at 2:39 pm

    Use a before_filter. This is called before every method call in your module:

    module StoreData
    
      before_filter :store_data_enabled
    
      def a
        ...
      end
    
      private
        def store_data_enabled
          STORE_DATA_ENABLED # return implicit true/false
        end
    end
    

    EDIT: a ruby-only approach. This uses the module initializer to re-create all public methods in the module and make them return nil. I did not try how this behaves when you have arguments in your methods.

    module M  
    
      # this is called while module init
      def self.included(base)
        unless STORE_DATA_ENABLED
          # iterate over public methods of this instance
          public_instance_methods.each do |m|
            # overwrite all methods, does not care about arguments
            define_method(m) { return nil }
          end
        end
      end
    
      def a
        ...
      end 
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a module import issue. using python 2.6 on ubuntu 10.10 I have
We have an issue using the PEAR libraries on Windows from PHP . Pear
I have the following issue using java 1.4 I try to display a very
I am having a issue using Google's SDC (Secure Data Connector) with my Google
I'm having an issue using the Cookie class of the Servlet API 2.5 on
I have an issue using jquerys selectors when it comes to a page I
I am investigating a Java issue (using IBM JVM 1.4.2 64-bit) on Red Hat
I believe I am having a memory issue using numpy arrays. The following code
I have an issue with using AWK to simply remove a field from a
I have an odd issue while using FlashCS4. I have a textfield that, when

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.