如何让我的Ruby gem“Mechanize”工作?

问题描述 投票:2回答:1

我需要为我正在使用的类安装Ruby gem'Mechanize'并使用它来完成一个项目。我正在使用Windows,我尝试使用网站rubyinstaller.org中的devkits安装2.6.1和2.5.3版本。安装这些版本之后,我完成了“gem install mechanize”并正确安装了gem。然而,当我尝试使用甚至“require'mechanize'”时,我得到了一个相当大的堆栈跟踪,我无法弄清楚出了什么问题。我试图多次卸载并重新安装所有内容。

堆栈跟踪:

C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/net-http-persistent-3.0.0/lib/net/http/persistent.rb:205:in `<class:Persistent>': uninitialized constant Process::RLIMIT_NOFILE (NameError)
    from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/net-http-persistent-3.0.0/lib/net/http/persistent.rb:190:in `<top (required)>'
    from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/mechanize-2.7.6/lib/mechanize.rb:6:in `<top (required)>'
    from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:135:in `require'
    from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:135:in `rescue in require'
    from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:39:in `require'
    from test.rb:1:in `<main>'

任何帮助或建议将不胜感激。谢谢!

ruby rubygems mechanize
1个回答
3
投票

看来这是一个已知的windows问题,其中一个gem的依赖项,请参阅:uninitialized constant Process::RLIMIT_NOFILE (NameError)

您可以尝试在那里使用hack将此行放在您的要求之前

Process::RLIMIT_NOFILE = 7 if Gem.win_platform?
require 'mechanize'

你也可以尝试在某种虚拟化环境中运行ruby,如果你真的想要成为一个ruby dev并且你必须使用windows。见Developing in Ruby on Windows

更新:这是一个已知的问题,不是机械化,但其中一个'依赖关系参见this issue和建议的解决方法:

找到mechanize gem的源路径。您应该找到运行结果部分中显示的路径之一

 gem env
 #look for a section that says:
 - GEM PATHS:

CD到步骤1的路径,然后在文本编辑器中输入lib/net/http/persistent.rb:并修改:

查找并删除此行:

DEFAULT_POOL_SIZE = Process.getrlimit(Process::RLIMIT_NOFILE).first / 4

然后在其中添加以下内容:

if Gem.win_platform? then 
  DEFAULT_POOL_SIZE = 256
else
  DEFAULT_POOL_SIZE = Process.getrlimit(Process::RLIMIT_NOFILE).first / 4
end

并保存文件。请记住,如果您使用bundler并计划运行bundle updatebundle upgrade,您将丢失这些更改。但这有望在未来的某个版本中修复。我没有测试过这个,因为我不使用Windows,但你可以尝试一下。

© www.soinside.com 2019 - 2024. All rights reserved.