[Code] FAQ: Detect if plugin is running on the Mac vs PC ?
-
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:
<span class="syntaxdefault">MAC </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> Object</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">RUBY_PLATFORM </span><span class="syntaxkeyword">=~</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">/(</span><span class="syntaxdefault">darwin</span><span class="syntaxkeyword">)/</span><span class="syntaxdefault">i </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> true </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> false </span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> unless defined</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">MAC</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">WIN </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> not MAC </span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> unless defined</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">WIN</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">OSX </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> MAC unless defined</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">OSX</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">PC </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> WIN unless defined</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">PC</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span>
- 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:<span class="syntaxdefault">if MAC<br /> dialog</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">show_modal</span><span class="syntaxkeyword">()<br /></span><span class="syntaxdefault">elsif WIN<br /> dialog</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">show</span><span class="syntaxkeyword">()<br />else<br /></span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">messagebox</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'Unsupported Platform for UI;;WebDialog class.'</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">end</span>
or:
if WIN require('WIN32OLE') else puts("Win32OLE.so is a Windows only utility!", MB_OK) end
DO NOT
<span class="syntaxdefault">PLATFORM </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">Object</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">RUBY_PLATFORM </span><span class="syntaxkeyword">=~</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">/</span><span class="syntaxdefault">mswin</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">i</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">;</span><span class="syntaxdefault">windows </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">((</span><span class="syntaxdefault">Object</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">RUBY_PLATFORM </span><span class="syntaxkeyword">=~</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">/</span><span class="syntaxdefault">darwin</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">i</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">;</span><span class="syntaxdefault">mac </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">;</span><span class="syntaxdefault">other</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span>
The example overwrites (reassigns,) the value of the standard (although deprecated,) Ruby constant
PLATFORM
. The example should use another constant name, perhapsPLAT_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 newString
instance object, whenever it encounters a literal quoted text in your code. (In this example, Ruby must create TWO newString
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 aString
during a boolean test. It is better (if you absolutly need to use aSymbol
,) to doSymbol
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]
-
(31MAR2012): The FAQ page has at last been updated ! (Thanks Simone.)
-
-
@thomthom said:
@dan rathbun said:
(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 customCURRENT_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.
-
I don't see the point at the
Symbol
pattern he used... -
Neither do I, as I had said in the OP.
Advertisement