• Login
sketchucation logo sketchucation
  • Login
🔌 Quick Selection | Try Didier Bur's reworked classic extension that supercharges selections in SketchUp Download

Aliasing UI::messagebox ?

Scheduled Pinned Locked Moved Developers' Forum
4 Posts 3 Posters 432 Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    scottlininger
    last edited by 22 Jan 2009, 23:44

    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,

    • Scott Lininger
      SketchUp Software Engineer
      Have you visited the Ruby API Docs?
    1 Reply Last reply Reply Quote 0
    • T Offline
      todd burch
      last edited by 23 Jan 2009, 03:43

      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") 
      
      
      1 Reply Last reply Reply Quote 0
      • S Offline
        scottlininger
        last edited by 23 Jan 2009, 05:08

        Thanks, Todd! That'll do nicely.

        • Scott Lininger
          SketchUp Software Engineer
          Have you visited the Ruby API Docs?
        1 Reply Last reply Reply Quote 0
        • D Offline
          Dan Rathbun
          last edited by 28 Dec 2009, 22:56

          @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.methodcall

          However.. 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.

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement