Ruby的杂项

本文介绍了Ruby的Gem包管理,包括Gemfile配置、错误处理,如gem安装报错、Dev-Kit问题和无效gem包错误。提供了详细解决方案,如修改源地址、设置ARCHFLAGS环境变量、安装和配置DevKit,以及删除corrupt的gem包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Rubu包管理

Ruby的软件包单元为RubyGem

  • Gem由.gemspec文件描述
  • Gem的构建过程由Rakefile描述
  • Rake是Gem的构建工具,它与Make类似,用以完成自动化测试和代码生成
  • Bundle则是Ruby的包管理工具,用来跟踪和下载正确版本的Gem

gem安装报错

gem install rails

报错如下:

ERROR:  Could not find a valid gem 'rails' (>= 0), here is why:Unable to download data from https://rubygems.org/ - Errno::ETIMEDOUT: Connection timed out - connect(2) for "s3.amazonaws.com" port 443 (https://api.rubygems.org/latest_specs.4.8.gz)

在stackoverflow上找到了答案:

这里写图片描述

大概意思是将https替换成http:

sudo gem sources -r https://rubygems.org/
sudo gem sources -a http://rubygems.org/

windows下如果出现这个错误,也是执行以上两句,去掉sudo即可。

另: 附上gem源有关的常用命令

  • 查看当前使用的源地址:gem sources
  • 删除默认的源地址:gem sources -r xxx.a.b.c/

    注:默认的url地址后必须有”/”,否则可能删不掉。

  • 添加源地址:gem sources -a xxx.a.b.c/

    注:国内使用淘bbbb宝的源比较稳定,且安装或更新网速都比较快。
    gem sources -a http://ruby.taobao.org/

注:Mac上执行sudo gem install rails报错

报错如下:

Building native extensions.  This could take a while...
ERROR:  Error installing rails:
    ERROR: Failed to build gem native extension.

在stackoverflow上找到了答案:

原因 是缺少Command Line Developer Tools

解决方案:(推荐第一种,第三种可能不奏效)

  • xcode-select --install
  • 去官网下载安装Apple XCode命令行工具
  • if you still have the problem even after Upgrading Xcode. Its may be because : 
    The Apple LLVM compiler in Xcode 5.1 treats unrecognized command-line options as errors. This issue has been seen when building both Python native extensions and Ruby Gems, where some invalid compiler options are currently specified.
    To workaround this issue, set the ARCHFLAGS environment variable to downgrade the error to a warning.
    ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future gem install GemName
    Personally I have got this problem while installing json gem , I did : ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future gem install json
    And the problem was solved.

删除命令行工具

如果因任何原因,你需要卸载命令行开发工具 ,找到/Library/Developer/ ,并删除该目录下的CommandLineTools文件夹。

其实我更愿意将/Library/Developer/Developer文件夹一同删除。因为没装命令行工具的时候,/Library/目录下没有Developer这个文件夹;装了命令行工具才出现的。

报Dev-Kit的错

经过上面一步解决了gem源的问题之后,还可能出现下面错误:

ERROR:  Error installing rails:
        The 'websocket-driver' native gem requires installed build tools.

Please update your PATH to include build tools or download the DevKit
from 'http://rubyinstaller.org/downloads' and follow the instructions
at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit'

出错的原因是安装XXXXX的时候,需要build tools,但系统中没有。

在stackoverflow上找到了答案:

这里写图片描述

大致意思如下:

  1. http://rubyinstaller.org/downloads/ 去下载dev kit – DevKit-mingw64-64-4.7.2-20130224-1432-sfx

  2. 按照 http://github.com/oneclick/rubyinstaller/wiki/Development-Kit/安装dev kit

安装步骤如下:

  1. 如果原来系统中已经安装了旧版的dev kit, 则删除它
  2. 下载上面提到的dev kit
  3. 解压下载下来的文件到指定的目录,如c:/devkit。(注意:目录不能有空格)
  4. 在安装目录下运行ruby dk.rb,然后按照提示分别运行ruby dk.rb initruby dk.rb install来增强ruby
  5. 可以运行gem install rdiscount –-platform=ruby来测试是否成功

按照安装步骤,完成了DevKit的安装,非常简单。

invalid gem: package is corrupt的错误

安装ruport时报了个错如下:

ERROR:  Error installing ruport:
        invalid gem: package is corrupt, exception while verifying: undefined me
thod `size' for nil:NilClass (NoMethodError) in D:/Ruby-2.2.5/Ruby22-x64/lib/rub
y/gems/2.2.0/cache/pdf-writer-1.1.8.gem

在stackoverflow上找到了答案,解决办法如下:

将%RUBY_HOME%/lib/ruby/gems/2.2.0/cache 文件夹中对应的gem删掉即可(这里是pdf-writer-1.1.8.gem),再重新执行命令。

Ruby的gem包管理

配置好Gemfile:

gemfile中可以定义项目中必须要安装的包以及相应的版本号,这样就可以避免不同的安装包以及版本之间的差异带来的问题。

例如:

source "http://rubygems.org"  

gem 'rake'  
gem 'foobar'  
gem 'foobar', '0.0.18'  

windows环境需要安装devkit

按前面的步骤配置好了devkit之后,执行gem install bundle安装bundle包;切换到Gemfile所在的目录,执行bundle install,Gemfile中指定的包以及相关的依赖包都会被安装。

导出当前机器上的gem包到某个文件

gem list > ~/gem_list.txt

Gemfile有了变动,更新

bundle update

不安装某个gem包对应的文档

gem install package-name --no-ri --nordoc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值