I’m creating a gem and have been testing it on MRI, JRuby, and Rubinius.
I have a section of code which checks to see if the Linux/Unix cli
command ‘factor’ exists on the platform the gem is installed on. If it
exists I wrap some ruby around it and use it. If ‘factor’ doesn’t exists
then I create pure ruby versions of the same methods.
To do this, I invoke the ‘factor’ command. If it exists I create the
methods I want using it. If it doesn’t exists, an error is thrown which
I use a ‘rescue’ section to resolve to create pure ruby versions.
Below is code to show the use of ‘rescue’:
If the system is a RUBY_PlATFORM which has ‘factor’ (Linux/Unix)
then the line: factor 10
.split(’ ') != []
will be performed with no error, and the code will proceed in sequence.
If the system doesn’t have ‘factor’ a sys error is thrown and ‘rescued’.
I test this on my Linux system by running it with the correct spelling
of ‘factor’, which outputs ‘OS versions’.
I then create a false cli command ‘factorz’ and run the code, and for
MRI (2.2.1 and 2.1.2) and Rubinius (2.5.2) a sys error is thrown and the
‘rescue’ section is invoked correctly and ‘Ruby versions’ is shown.
However, on JRuby 1.7.19, in both cases I get ‘OS versions’.
So apparently, JRuby isn’t throwing a system error when it attempts to
run a non-existent cli command, which doesn’t conform to MRI (2.2; 2.1).
1)Is this a bug in JRuby, or is this allowed behavior?
2)Is there a ‘better’ more universally transferable way to do this?
begin
# Test Operating Systems if *nix cli command 'factor' exists
`factor 10`.split(' ') != []
#`factorz 10'.split(' ') != [] # mimic nonexistent on system
# code would go here to create 'factor' coded methods
puts 'OS versions'
rescue # if ‘factor’ not in system use pure ruby versions
# code would go here to create pure ruby coded methods
puts 'Ruby versions'
end