为什么“要求rubygems”是错误的? [英] Why is it "wrong to require rubygems"?

查看:127
本文介绍了为什么“要求rubygems”是错误的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这个帖子,要求rubygems是一个反模式。

  require'rubygems'



<这个论点似乎归结为:


当我使用你的库时,部署你的
应用,或者运行你的测试我可能不希望
使用rubygems。当你在代码中需要
'rubygems'时,你可以删除我的
来做出这个决定。我
不能不要求rubygems,但你可以
不要求它在第一位。


但是couldn'当你创建并与其他人共享代码库时,你需要的任何Ruby库都有相同的参数? 解决方案

需要使用rubygems的主要问题是,即使它们确实有实际需要的库,它也会在没有安装rubygems的系统上导致错误。



有些人在需要rubygems的时候通过解决LoadError来解决这个问题,但这很糟糕。


According to this post, requiring rubygems is an antipattern.

require 'rubygems'

The argument seems to boil down to this:

When I use your library, deploy your app, or run your tests I may not want to use rubygems. When you require 'rubygems' in your code, you remove my ability to make that decision. I cannot unrequire rubygems, but you can not require it in the first place.

But couldn't the same argument be made about any Ruby library that you 'require' when you create and share a library of code with other people?

解决方案

The main problem with requiring rubygems is that it will cause an error on systems that do not have rubygems installed even if they do have the library that you actually want to require.

Some people work around that by rescuing LoadError when requiring rubygems, but that's kind of ugly.

这篇关于为什么“要求rubygems”是错误的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆