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

  • SEARCH
  • Home
  • 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 821203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:36:12+00:00 2026-05-15T02:36:12+00:00

Greetings, I want to tinker with the global memcache object, and I found the

  • 0

Greetings,

I want to tinker with the global memcache object, and I found the following problems.

  1. Cache is a constant
  2. Cache is a module

I only want to modify the behavior of Cache globally for a small section of code for a possible major performance gain.

Since Cache is a module, I can’t re-assign it, or encapsulate it.

I Would Like To Do This:

Deep in a controller method…

code code code...

old_cache = Cache
Cache = MyCache.new

code code code...

Cache = old_cache

code code code...

However, since Cache is a constant I’m forbidden to change it. Threading is not an issue at the moment. 🙂

Would it be “good manners” for me to just alias_method the special code I need
just for a small section of code and then later unalias it again? That doesn’t
pass the smell test IMHO.

Does anyone have any ideas?

TIA,

-daniel

  • 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-15T02:36:12+00:00Added an answer on May 15, 2026 at 2:36 am

    But you can overwrite constants in Ruby (regardless of whether it’s a module or class or simple other object):

    MyConst = 1
    
    # do stuff...
    
    old_my_const = MyConst
    MyConst = 5
    puts "MyConst is temporarily #{MyConst}"
    MyConst = old_my_const
    
    puts "MyConst is back to #{MyConst}"
    

    Output:

    a.rb:6: warning: already initialized constant MyConst
    MyConst is temporarily 5
    a.rb:8: warning: already initialized constant MyConst
    MyConst is back to 1
    

    The warnings are simply that: warnings. Your code will continue to run the same.

    Okay, maybe the warnings are unacceptable in your situation for some reason. Use this suppress_all_warnings method I’ve written. Example includes reassigning a module.

    def suppress_all_warnings
      old_verbose = $VERBOSE
      begin
        $VERBOSE = nil
        yield if block_given?
      ensure
        # always re-set to old value, even if block raises an exception
        $VERBOSE = old_verbose
      end
    end
    
    module OriginalModule
      MyConst = 1
    end
    
    module OtherModule
      MyConst = 5
    end
    
    def print_const
      puts OriginalModule::MyConst
    end
    
    print_const
    
    suppress_all_warnings do
      old_module = OriginalModule
      OriginalModule = OtherModule
    
      print_const
    
      OriginalModule = old_module
    end
    
    print_const
    

    Now you get the correct output, but without the warnings:

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

Sidebar

Related Questions

Greetings to all... I want to use fade effect with jCarouselLite qualities. Is it
Greetings, I am trying to learn pointers in C, I simply want my addtwo
greetings, I'm new with java script so bear with me! I want to achieve
Greetings, I have a method to capture packets. After capturing I want to add
Greetings, I want to store some data in a redis db and don't know
Greetings, By using Django, I want to generate a tar.gz which contains the files
Greetings, I use ASP.NET validation control and I want to reset them whenever the
Greetings, how can I inject c# code (my Model property) inside javascript? I want
greetings all i have a domain class named car and i have an object
Greetings all, I want to run the code block inside the loop ,in seperate

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.