Dir.bitbucket?

D

Daniel Berger

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.

I think the code, using pure Ruby, would look like this:

case RUBY_PLATFORM
when /mswin/i
'NUL'
when /amiga/i
'NIL:'
when /openvms/i
'NL:'
else
'/dev/null'
end

I based this on http://en.wikipedia.org/wiki//dev/null

Useful? If so, we could easily patch dir.c, though I'm not sure what
the best way to approach it would be, i.e. should we do platform checks
within the code or rely on constants set during the build phase?

Regards,

Dan
 
L

Luke Kanies

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 :eek:peratingsystem => :mswin
setcode { "NUL" } # strings get executed as shell code, so you have
to use a block
end

Facter.add :bitbucket do
confine :eek:peratingsystem => :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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,089
Members
47,689
Latest member
kilaocrhtbfnr

Latest Threads

Top