On Aug 19, 2006, at 11:34 PM, Daniel B. wrote:
Hi all,
Occasionally I have to do something like this:
bitbucket = RUBY_PLATFORM.match(‘mswin’) ? ‘NUL’ : ‘/dev/null’
How about a Dir.bitbucket method? Or Dir.null, or whatever you want
to call it.
it’s not quite the same thing, since it’s not a part of the Ruby
core, but I’ve got a library, Facter[1], specifically meant to help
handle this kind of platform variety. This kind of simple code would
look like this:
Facter.add :bitbucket do
setcode do
case Facter.operatingsystem
when /mswin/i: ‘NUL’
when /amiga/i: ‘NIL’
when /openvms/i: ‘NL:’
else
‘/dev/null’
end
end
end
The reason the code is set separately is that, for more complicated
facts, you could have a separate resolution mechanism for every
platform:
Facter.add :bitbucket do
confine :operatingsystem => :mswin
setcode { “NUL” } # strings get executed as shell code, so you have
to use a block
end
Facter.add :bitbucket do
confine :operatingsystem => :amiga
setcode { “NIL” }
end
…
Any fact can be be confined based on other facts, so you could
confine a fact to work only on a given release of a specific platform
(e.g., Mac OS X seems to change the config file that stores the IP
configuration on every release).
The interface could probably be slimmed down some, but the basic idea
is there: Make it simple to get a fact on different platforms. It’s
especially useful for things like domain names, which can often be
resolved multiple ways (/etc/resolv.conf, /bin/domainname, dns, /etc/
hosts, etc.)
1 - http://reductivelabs.com/projects/facter