Greetings,
I want to tinker with the global memcache object, and I found the following problems.
- Cache is a constant
- 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
But you can overwrite constants in Ruby (regardless of whether it’s a module or class or simple other object):
Output:
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_warningsmethod I’ve written. Example includes reassigning a module.Now you get the correct output, but without the warnings: