How to determine if ruby ​​can use fork without excessive use of regexp?

Is it possible to determine if the ruby ​​implementation you are using supports forkwithout running a regular expression against RUBY_PLATFORM, which will expand as long as it calls Cthulhu ?

(Related question: Ruby - How do I know which system my program is running on?

Change . I tried Mark-Andre's suggestion. It does not work for jruby with the plug disabled by default:

192-168-1-7:~ agrimm$ jruby --1.9 -S jirb 
irb(main):001:0> RUBY_VERSION
=> "1.9.2dev"
irb(main):002:0> Process.respond_to?(:fork)
=> true
irb(main):003:0> Process.fork
NotImplementedError: fork is unsafe and disabled by default on JRuby

Update : using the Marc-Andre link, this seems like a wiser voice than I dealt with this problem in terms of creating Ruby implementations and failed.

From the point of view of someone writing a ruby ​​library, what would be the most complete spell would be the lack of a plug and seeing if this throws an exception?

+3
source share
3 answers

In Ruby 1.9:

Process.respond_to?(:fork)  # => true if fork is supported, false otherwise

For Ruby 1.8 or JRuby (which does not currently implement this) you will need to test it.

See also the long discussion on ruby-core.

+3
source

Instead of error-prone testing with RUBY_PLATFORM or other things, you can test forkyourself:

def can_fork?
  pid = fork
  exit unless pid # exit the child immediately
  true
rescue NotImplementedError
  false
end

One drawback would be if forkit is somehow emulated, which can make this verification expensive.

+1
source

A rails

Config::CONFIG['host_os'] !~ /mswin|mingw/)

, Windows,

RUBY_PLATFORM !~ /java/

, JRuby. , fork . , , .

I suspect that macruby does not support or strongly discourages branching, and from the thread referenced by Marc-Andre, you can check it with

 RUBY_ENGINE != "macruby"

Now for all other platforms out there ... (work in progress, designation as community wiki, please edit)

0
source

Source: https://habr.com/ru/post/1742463/


All Articles