Why is it “wrong to require rubygems”?

我只是一个虾纸丫 提交于 2020-01-02 01:19:09

问题


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?


回答1:


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.




回答2:


If you look at the comments, you will see the author say this

The point I was not trying to make is that rubygems is bad or that people shouldn’t use it. I use rubygems and think it’s a critical piece of Ruby’s success – I just don’t have the line “require 'rubygems'” anywhere in my library’s source code because it’s not necessary or convenient and makes it hard / impossible for other environments (like bundler, rip, GAE, debian, hand rolled, etc) to use the library without rubygems, which is often important.

I actually didn't really agree with him before I read it, now I do




回答3:


Rubygems isn't essential to the application, it's just a convenient way of supplying a library. By contrast, the other libraries used are essential to the application.



来源:https://stackoverflow.com/questions/1434259/why-is-it-wrong-to-require-rubygems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!