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)/i ? 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]