Sometimes I have seen following code.
gem 'factory_girl','= 1.2.3'
require 'factory_girl'
I tried to look at gem doc but could not find answer to the开发者_开发知识库 question of what does the first line do in code above?
What you're looking for in the gem docs is about Coding with Rubygems.
The first line basically says "Hey, go get this gem with this version" from the install directory for gems and load it into the environment. This is mainly to help you add version dependencies to your require
s instead of just doing require 'factory_girl'
by itself.
Edit: To add on to Jörg's point below, I thought that Ryan Tomayko had a pretty good short and sweet article about why doing this is "wrong".
As @theIV already explained, this activates the factory_girl
gem, using exactly (because of the =
sign) version 1.2.3
.
Note, however, that this is very bad practice and should never be done. If you activate gems manually inside your code, it means that people who do not use RubyGems can no longer use your code.
RubyGems is a package manager. Your code should never care about what package manager was used to install it. Some people prefer RubyGems, some dpkg/APT, some RPM/YUM, some RPM/APT, some RPM/URPMI, some RPM/YaST2, Portage, FreeBSD ports, pkgsrc, MacPorts, slashpackage, CoAPP, Conary, Slackware. There's tons of them. Some people like not to use any package manager at all. Or, they use RubyGems just for downloading, but then unpack the gem into their vendor
directory.
All of this cannot possibly work, if you use the gem
method in your code.
精彩评论