[Code] FAQ: Detect if plugin is running on the Mac vs PC ?

[Code] FAQ: Detect if plugin is running on the Mac vs PC ?

Postby Dan Rathbun » Wed Feb 08, 2012 1:48 pm

    FAQ: How can I detect if my plugin is running on the Mac vs. PC ?
    from: Frequently Asked Questions (FAQ)

    The answer (as of this posting date,) has a VERY POOR example. Do not use it.
    EDIT(31MAR2012): The FAQ page has at last been updated ! (Thanks Simone.)


    DO

    It is BEST to create Boolean constants for comparison:
    Code: Select all
    MAC = ( Object::RUBY_PLATFORM =~ /(darwin)/? true : false ) unless defined?(MAC)
    WIN = ( not MAC ) unless defined?(WIN)
    OSX = MAC unless defined?(OSX)
    PC = WIN unless defined?(PC) 
    • You can put these outside your author module (namespace,) so they are global constants
    • Or within anyone of your namespaces (submodules and/or classes,) so they are local.

    Then you can just have an if statement like:
    Code: Select all
    if MAC
      dialog
    .show_modal()
    elsif WIN
      dialog
    .show()
    else
      UI.messagebox('Unsupported Platform for UI::WebDialog class.')
    end

    or:
    Code: Select all
    if WIN
      require('WIN32OLE')
    else
      puts("Win32OLE.so is a Windows only utility!", MB_OK)
    end




    DO NOT

    Code: Select all
    PLATFORM = (Object::RUBY_PLATFORM =~ /mswin/i) ? :windows : ((Object::RUBY_PLATFORM =~ /darwin/i) ? :mac : :other) 

    The example overwrites (reassigns,) the value of the standard (although deprecated,) Ruby constant PLATFORM. The example should use another constant name, perhaps PLAT_SYM.

    Note also that the example creates interned String values that will later be used for comparison.
    Ruby is extremely SLOW at String comparison.
    Observe:
    PLAT_SYM.to_s == "windows"
    Is much slower than testing a boolean constant. Reason is that Ruby must create a new String instance object, whenever it encounters a literal quoted text in your code. (In this example, Ruby must create TWO new String instance objects, one on each side of the == operator. They are abandoned, aka unreferenced, after the line is evaluated. Ruby garbage collection will clean them up eventually. Sooner if the statement was within a method.)

    It also makes no sense, to assign Symbol constants, and then convert them to a String during a boolean test. It is better (if you absolutly need to use a Symbol,) to do Symbol comparison, like:
    PLAT_SYM == :windows




    Edited and re-organized, 09 FEB 2012.

    [Comment added to bottom of FAQ webpage and Documentation Bug Report filed. ~ Dan]
    0
    Last edited by Dan Rathbun on Mon Apr 02, 2012 5:27 pm, edited 1 time in total.
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: [Code] FAQ: Detect if plugin is running on the Mac vs PC

      Postby Dan Rathbun » Sun Apr 01, 2012 2:15 am

        (31MAR2012): The FAQ page has at last been updated ! (Thanks Simone.)
        0
          I'm not here much anymore. But a PM will fire email notifications.
          User avatar
          Dan Rathbun 
          PluginStore Author
          PluginStore Author
           

          Re: [Code] FAQ: Detect if plugin is running on the Mac vs PC

          Postby thomthom » Mon Apr 02, 2012 4:10 pm

          Dan Rathbun wrote:
            (31MAR2012): The FAQ page has at last been updated ! (Thanks Simone.)

            What was changed? The FAQ appear to use your "DO NOT" example... :?
            0
            Thomas Thomassen — SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund
            User avatar
            thomthom 
            PluginStore Author
            PluginStore Author
             

            Re: [Code] FAQ: Detect if plugin is running on the Mac vs PC

            Postby Dan Rathbun » Mon Apr 02, 2012 5:19 pm

            thomthom wrote:
            Dan Rathbun wrote:
              (31MAR2012): The FAQ page has at last been updated ! (Thanks Simone.)

              What was changed? The FAQ appear to use your "DO NOT" example... :?

              He changed the name of the constant from the Ruby PLATFORM to a custom CURRENT_PLATFORM (although he refused to give up the symbol type example,) he did add a (similar) boolean example as I gave it. (or close enough.)

              I guess it is the best I can hope for.
              0
                I'm not here much anymore. But a PM will fire email notifications.
                User avatar
                Dan Rathbun 
                PluginStore Author
                PluginStore Author
                 

                Re: [Code] FAQ: Detect if plugin is running on the Mac vs PC

                Postby thomthom » Mon Apr 02, 2012 6:19 pm

                I don't see the point at the Symbol pattern he used...
                0
                Thomas Thomassen — SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund
                User avatar
                thomthom 
                PluginStore Author
                PluginStore Author
                 

                Re: [Code] FAQ: Detect if plugin is running on the Mac vs PC

                Postby Dan Rathbun » Mon Apr 02, 2012 11:48 pm

                Neither do I, as I had said in the OP.
                0
                  I'm not here much anymore. But a PM will fire email notifications.
                  User avatar
                  Dan Rathbun 
                  PluginStore Author
                  PluginStore Author
                   

                  SketchUcation One-Liner Adverts

                  by Ad Machine » 5 minutes ago



                  Ad Machine 
                  Robot
                   



                   

                  Return to Developers' Forum

                  Who is online

                  Users browsing this forum: No registered users and 8 guests

                  Visit our sponsors: