如何确定 ruby 是否可以在不过度使用正则表达式的情况下进行分叉?
是否可以确定您正在运行的 ruby 实现是否能够支持 fork
,而无需针对 RUBY_PLATFORM
运行正则表达式,该正则表达式将扩展直到 召唤克苏鲁?
(相关问题:Ruby -我怎样才能知道我的程序正在哪个系统上运行?)
编辑:我尝试了马克-安德烈的建议。它不适用于默认禁用 fork 的 jruby:
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
更新:从 Marc-Andre 的链接来看,从创建 ruby 实现的角度来看,它似乎比我更明智地解决了这个问题,但失败了。
从编写 ruby 库的人的角度来看,除了运行 fork 并查看它是否引发异常之外,最全面的咒语是什么?
Is it possible to determine whether the implementation of ruby you're running on is capable of supporting fork
, without running a regex against RUBY_PLATFORM
that'll expand until it summons Cthulhu?
(Related question: Ruby - How can I find out on which system my program is running?)
Edit: I tried Marc-Andre's suggestion. It doesn't work for jruby with fork 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: From Marc-Andre's link, it seems wiser heads than I have grappled with this problem from the perspective of creating ruby implementations, and failed.
From the perspective of someone who's writing a ruby library, what would be the most comprehensive incantation, short of running fork and seeing if it raises an exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Ruby 1.9 中:
对于 Ruby 1.8 或 JRuby(当前未实现此功能),您必须实际测试它。
另请参阅关于 ruby-core 的长篇讨论。
In Ruby 1.9:
For Ruby 1.8, or JRuby (which doesn't implement this currently) you'll have to actually test it.
See also this long discussion on ruby-core.
您可以测试
fork
本身,而不是针对 RUBY_PLATFORM 或其他内容进行容易出错的测试:一个缺点是,如果以某种方式模拟
fork
,这可能会使此检查变得昂贵。Instead of error-prone testing against RUBY_PLATFORM or other things you could test
fork
itself:One downside would be if
fork
is somehow emulated which could make this check expensive.最近对 Rails 的提交用于
测试它是否不在 Windows 上,
并测试它是否不在 JRuby 上。但是,我知道可以启用
fork
作为命令行开关。我必须研究是否有任何方法可以判断它是否已启用。我怀疑 macruby 不支持或强烈反对分叉,并且从 Marc-Andre 提到的线程中,您可以使用
Now 对所有其他平台进行测试...(正在进行中,标记为社区 wiki ,请随意编辑)
A recent commit to rails uses
to test whether it's not on Windows, and
to test whether it's not on JRuby. However, I know it's possible to enable
fork
as a command-line switch. I'd have to look into whether there's any way of telling whether it's been enabled.I suspect macruby doesn't support, or strongly discourages, forking, and from the thread Marc-Andre refers to, you can test for it with
Now for all the other platforms out there... (work in progress, marking as community wiki, please feel free to edit)