【问题标题】:How can I get my Ruby gem 'Mechanize' to work?如何让我的 Ruby gem 'Mechanize' 工作?
【发布时间】:2019-07-01 02:19:15
【问题描述】:

我需要为我正在学习的课程安装 Ruby gem 'Mechanize' 并使用它进行项目。我正在使用 Windows,并尝试使用网站 ruby​​installer.org 的 devkit 安装 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】:

    这似乎是此 gem 的依赖项之一中的已知 Windows 问题,请参阅:uninitialized constant Process::RLIMIT_NOFILE (NameError)

    您可以尝试那里给出的 hack 将这一行放在您的要求之前

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

    如果您认真想成为一名 ruby​​ 开发人员并且必须使用 windows,您也可以尝试在某种虚拟化环境中运行 ruby​​。见Developing in Ruby on Windows

    更新:这不是 mechanize 的已知问题,而是其依赖项之一,请参阅this issue 和建议的解决方法:

    找到机械化宝石的来源路径。您应该在运行的结果部分中找到它的路径之一

     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,但是你可以试试。

    【讨论】:

    • 这为我解决了问题。 Windows 10 v1803 x64,使用来自 rubyinstaller.org 的 ruby​​installer-devkit-2.5.3-1-x64.exe。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 2012-11-18
    相关资源
    最近更新 更多