宝石文件夹是否特定?

huangapple go评论93阅读模式
英文:

Are gems folder specific?

问题

我有一个旧的Ruby on Rails项目,我作为一种爱好来进行开发,但我从未实施过Rspec测试。

现在我想这样做,所以我正在阅读精良的书《Effective Testing with Rspec 3》。我会假设Rspec书中使用的技巧和rspec gem适用于rspec-rails gem,因为我从rspec.info获取了rspec书,该网站仅提供rspec-rails及其依赖项的文档。我假设rspec-rails会自动在RoR项目中创建子文件夹和文件等等。我目前不确定我将使用哪一个来实施测试。

无论如何,显然我已经在我旧的爱好项目目录中安装了rspec(在项目目录中运行rspec -v):

3.5.4

而且rspec-rails也是项目的Gemfile中的一个gem(版本已经过时,但是在编写该书时是当前版本),根据gem list --local,它也随rspec一起安装了,但是当我在旧的项目目录内使用rspec-rails -v时,也会出现'command not found'的错误。

所以我创建了一个名为rspectest的新文件夹,我将在其中实施书中的示例和说明。然而,按照第1章的说明:

$ gem install rspec -v 3.6.0
成功安装了rspec-support-3.6.0
成功安装了rspec-core-3.6.0
成功安装了diff-lcs-1.3
成功安装了rspec-expectations-3.6.0
成功安装了rspec-mocks-3.6.0
成功安装了rspec-3.6.0
6个gem已安装

我尝试在我的新的rspectest文件夹中类似地安装rspec,但是我收到错误:

ERROR:  Could not find a valid gem 'rspec' (= 3.5.4) in any repository
ERROR:  Possible alternatives: r_spec

我之所以提问的原因是因为在第7页上要求运行命令rspec从项目目录中,以查看示例的结果,但我得到:

找不到命令'rspec',但可以使用以下命令安装:

sudo apt install ruby-rspec-core

所以我必须假设这个命令只能从特定的新rspec测试文件夹中访问gem,如果从该目录内键入命令(?):

sudo apt install ruby-rspec-core

并且不会干扰不同项目文件夹的gems,我假设这是gemfile等的目的,以防本地安装不同版本的gems。

...我差不多弄明白了,如果上述命令不起作用,我会删除这个问题。编辑:在回答以下问题之前等待答案:

1)一般来说,所有文件夹是否相互独立,就gem及其可访问性而言?

2)我是否需要在特定文件夹中使用bundle来安装gem?我认为不需要,因为有gem install这个选项,而这需要设置一个Rails项目,虽然我可以这样做,但这只是书中的第1个示例。

3)如果我在一个随机文件夹中运行sudo apt install ruby-rspec-core(并且它安装了另一个版本或其他奇怪的东西),是否会影响我的旧项目文件夹?

我也认为不会,但最好澄清为什么书中的示例不会立刻生效。我不喜欢在开始实验时不得不重新安装存储库或擦除驱动器,尤其是在第4页。

英文:

I have an old Ruby on Rails project that I work on as a hobby, but I have never implemented Rspec testing.

Now I would like to do so, so I am reading the fine book 'Effective Testing with Rspec 3'. I would assume the techniques used in the Rspec book and the rspec gem are applicable to the rspec-rails gem, because I got the rspec book from rspec.info, which only offers documentation for rspec-rails and its dependencies. I assume rspec-rails auto-creates the sub folders and files in a RoR project etc. Which one I'm going to use to implement the tests I'm not sure at this point.

Anyway I apparently have rspec installed in my old hobby PROJECT directory (rspec -v from the project directory)

3.5.4

and rspec-rails is also a gem inside the project Gemfile (an outdated version, but current to the time of writing the book), which is also installed along with rspec, according to gem list --local, but I also get 'command not found' with rspec-rails -v from within the old project directory.

So I have created a new folder called rspectest, in which I will use to implement the examples and instructions in the book. However, following the instructions in Chapter 1:

$ gem install rspec -v 3.6.0
Successfully installed rspec-support-3.6.0
Successfully installed rspec-core-3.6.0
Successfully installed diff-lcs-1.3
Successfully installed rspec-expectations-3.6.0
Successfully installed rspec-mocks-3.6.0
Successfully installed rspec-3.6.0
6 gems installed

I have attempted to similarily install rspec in my new rspectest folder, but I get error:

ERROR:  Could not find a valid gem 'rspec' (= 3.5.4) in any repository
ERROR:  Possible alternatives: r_spec

The reason I ask is because on page 7 it asks to run the command rspec from the project directory, to see the results of an example, and I get:

Command 'rspec' not found, but can be installed with:

sudo apt install ruby-rspec-core

So I have to assume this command will allow access to the gem from this particular new rspec testing folder only, if typed from within the directory (?):

sudo apt install ruby-rspec-core

And will not mess with the gems of different project folders, which I assume is the point of gemfile etc, in case different versions of gems are installed locally.

... guess I about figured that one out, my apologies. I'll delete this unless the above command doesn't work. EDIT: pending answers to these questions:

  1. Are all folders independent of each other, generally, as far as gems and their accessibility?

  2. Do I need bundle to install a gem in a particular folder? I assume not since gem install is a thing, and would require setting up a Rails project, which I could do, but it's literally example #1 in the book.

  3. If I go sudo apt install ruby-rspec-core in a random folder (and it installs another version or something weird), is that going to affect my old project folder?

I also assume not, but it'd be nice to clarify why the book examples aren't working off the bat. I dislike having to reinstall the repo or having to wipe the drive when i start experimenting, especially when it's only on page 4

答案1

得分: 1

你的问题有点混淆,但我会为你提供有关如何在你的计算机上管理宝石(Gems)的一般概述。

通常在一个Rails项目中,有一个Gemfile文件,它定义了你的Ruby版本以及项目所需的宝石。有时,它们会指定特定的版本,如'gem 'aws-ses-v4', '~> 0.8.1'

此外,使用像rvm或rbenv(我更喜欢rbenv)这样的工具来管理Ruby是一个好的实践。这对于在你的计算机上拥有多个Ruby版本非常有用。对于rbenv,你会有一个.ruby-version文件,用来设置Ruby版本。每当你在那个项目目录中运行一个命令时,rbenv都会检查该文件,以确定它正在使用哪个Ruby版本以及它应该在哪里查找宝石等。

现在,如果你没有使用这些工具,你将需要在终端中手动设置你正在使用的Ruby版本,我怀疑你遇到的问题是你有多个Ruby版本,其中一个安装了rspec,而另一个没有。

除了bundler之外,你通常不需要手动运行gem install命令,只需要使用Gemfile和bundle install来安装所有其他宝石。

如果你手动运行gem install命令,它将把宝石放在你当前正在使用的Ruby版本的本地目录中。这个目录的位置因你使用的计算机类型而异。对于我来说,使用rbenv,它位于~/.rbenv/versions/2.3.8/lib/ruby/gems/2.3.0/gems,但默认情况下可能不同。

英文:

So your question is a bit confusing but I'll just give you a general overview of how gems are managed on your machine.

Typically in a rails project there is a Gemfile that defines your ruby version as well as the gems you need for your projects. Sometimes they will have specific version specified as well 'gem 'aws-ses-v4', '~> 0.8.1'

Also it is good practice to use something like either rvm or rbenv(my preference) to handle ruby management. This is useful for having multiple versions of ruby on your computer. For rbenv you will have a .ruby-version file that you use to set the ruby version and anytime you are in that project directory and run a command rbenv will check that file to determine which ruby version its using and where it should look to find gems, etc.

Now if you aren't using that you are going to need to manually set what version of ruby you are using in terminal and what I'm suspecting you have happening is that you have multiple ruby versions and one of which has rspec installed while the other doesn't.

You should never really need to manually run gem install besides for bundler, and then use a Gemfile and bundle install for everything else.

If you are manually running the gem install command its going to drop the gem inside of the your local directory for the ruby version you are currently using. This will be different based of what kind of computer you are using. For me using rbenv its ~/.rbenv/versions/2.3.8/lib/ruby/gems/2.3.0/gems but not overly sure where it is by default.

huangapple
  • 本文由 发表于 2023年7月18日 06:00:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定