Aliasing UI::messagebox ?
-
Hey guys,
So I'm trying to override the UI::messagebox method so it doesn't bark at me while I'm running a bunch of unit tests. That bit was easy...
def UI;;messagebox(params) puts 'TEMPORARY OVERRIDE; UI;;messagebox > ' + params.to_s end
So far so good. But once I'm done with my tests, I'd like to return control back to the default UI.messagebox. I tried various flavors of the alias and alias_method keywords, but those don't seem to work with modules. I'm certain there's a way to do it, but I thought I'd ask here before diving into my Ruby books. Hopefully it's a simple syntax hiccup.
Thanks in advance, oh Ruby masters,
-
This will do it. I wish there was an unalias_method. I got around it with a global.
# ruby example to temporarily override a method require 'sketchup.rb' $no_msgbox = true ; module UI alias_method ;messagebox_real, ;messagebox def UI.messagebox(string) if $no_msgbox then puts "UI;;messagebox -> #{string}" else messagebox_real(string) end end # def end # module include UI UI.messagebox "see me print in the console..." ; $no_msgbox = false ; UI.messagebox("Back to the real messagebox")
-
Thanks, Todd! That'll do nicely.
-
@unknownuser said:
... I tried various flavors of the alias and alias_method keywords, but those don't seem to work with modules. I'm certain there's a way to do it, but I thought I'd ask here before diving into my Ruby books.
Yes.. it's weird that alias doesn't work with modules.
But this is even weirder. To alias modules, you instead use the object= method.
Example aliasing of Sketchup module: (At Ruby Console type the following, where ">>" is resulting output.)
Sketchup.class >> Module Sketchup.object_id >> 43353420 # remember this id... # now alias the module SU = Sketchup >> Sketchup SU.class >> Module SU.object_id >> 43353420 # it's the SAME exact id # test it... SU.version >> 7.1.6087
I would think that we must be careful what namespace the alias constant is declared within. If the above statement "SU = Skecthup" was declared inside a module, or a class, it would only be accessible within that namespace, or must be qualified to be accessed.
For example, say you used that alias assignment within a module named "Configurator", in order to access the alias from outside, you'd need to qualify it with:
Configurator::SU.methodcallHowever.. if you wished the alias to have global public access, you'd need to declare it outside ALL modules, in the objectspace. (Similar to typing it at the console.)
BUT... what if for control purposes (ie, conditional loading,) you needed to have the statement inside a class or module?I think in this case, remember the Kernel.eval() method, and the fact that module Kernel is included in every object. That means that the containing module has it's own copy of eval. So you cannot call just eval (unqualified,) because you'd be calling the local module's copy of eval.
To do it from within a module (or class,) you must qualify the call:
if situation then Kernel.eval("SU = Sketchup");
Then the alias constant is global.
Advertisement