FAQ: How can I detect if my plugin is running on the Mac vs. PC ?
from: Frequently Asked Questions (FAQ)<http://developers.google.com/sketchup/docs/faq>
The answer (as of this posting date,) has a VERY POOR example. Do not use it
.
------------------------------
DO
It is BEST to create Boolean constants for comparison:
CODE: SELECT ALL<http://forums.sketchucation.com/viewtopic.php?f=180&t=34631#>
1. MAC = ( Object::RUBY_PLATFORM =~ /(darwin)/i ? true : false ) unless
defined?(MAC)
2. WIN = ( not MAC ) unless defined?(WIN)
3. OSX = MAC unless defined?(OSX)
4. 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
- If you put these statements, within YOUR toplevel "Author" module,
then the platform constants, will be accessible, to all of your nested
modules and classes.
Then you can just have an if statement like:
CODE: SELECT ALL<http://forums.sketchucation.com/viewtopic.php?f=180&t=34631#>
1. if MAC
2. dialog.show_modal()
3. elsif WIN
4. dialog.show()
5. else
6. UI.messagebox('Unsupported Platform for UI::WebDialog class.')
7. end
or:CODE: SELECT ALL<http://forums.sketchucation.com/viewtopic.php?f=180&t=34631#>
1. if WIN
2. require('WIN32OLE')
3. else
4. puts("Win32OLE.so is a Windows only utility!", MB_OK)
5. end
------------------------------
DO NOT
CODE: SELECT ALL<http://forums.sketchucation.com/viewtopic.php?f=180&t=34631#>
1. 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
~