陷入 rvm 地狱,试图让一个简单的 rspec 运行

发布于 2024-12-26 00:41:42 字数 1534 浏览 2 评论 0原文

来自终端的 Ruby 设置

 % ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [i686-linux]
=> ~/ruby/grounded/test
 % where ruby
/home/mike/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
/home/mike/.rvm/bin/ruby
/usr/local/bin/ruby
/usr/bin/ruby
=> ~/ruby/grounded/Person/test
 % which ruby
/home/mike/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
% rvm current
ruby-1.9.2-p180

目录结构

 % tree
.
├── bowling.rb
└── bowling_spec.rb

文件内容

Bowling.rb

class Bowling

end

Bowling_spec.rb

require 'rubygems'
require 'rspec'
require 'bowling'
describe Bowling, "#score" do
  it "returns 0 for all gutter game" do
    bowling = Bowling.new
    20.times { bowling.hit(0) }
    bowling.score.should eq(0)
  end
end

% ruby​​ Bowling_spec.rb

/home/mike/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': no such file to load -- bowling (LoadError)
    from /home/mike/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from bowling_spec.rb:3:in `<main>'

问题

  • 为什么当 bowling.rbbowling_spec.rb 时,我收到 LoadError 位于同一个文件夹中?
  • 当我运行 ruby​​ 1.9.2 时,在错误中到底是什么 .../site_ruby/1.9.1/... 那么为什么 1.9.1 甚至会显示向上?
  • 我该如何克服这个困难并开始享受 ruby​​ 带来的乐趣。

Ruby Settings From terminal

 % ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [i686-linux]
=> ~/ruby/grounded/test
 % where ruby
/home/mike/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
/home/mike/.rvm/bin/ruby
/usr/local/bin/ruby
/usr/bin/ruby
=> ~/ruby/grounded/Person/test
 % which ruby
/home/mike/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
% rvm current
ruby-1.9.2-p180

Directory Structure

 % tree
.
├── bowling.rb
└── bowling_spec.rb

File Contents

bowling.rb

class Bowling

end

bowling_spec.rb

require 'rubygems'
require 'rspec'
require 'bowling'
describe Bowling, "#score" do
  it "returns 0 for all gutter game" do
    bowling = Bowling.new
    20.times { bowling.hit(0) }
    bowling.score.should eq(0)
  end
end

% ruby bowling_spec.rb

/home/mike/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': no such file to load -- bowling (LoadError)
    from /home/mike/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from bowling_spec.rb:3:in `<main>'

Questions

  • Why am I getting a LoadError when bowling.rb and bowling_spec.rb
    are in the same folder?
  • In the error what the heck is .../site_ruby/1.9.1/... when I am running ruby 1.9.2 then why would 1.9.1 even show up?
  • how do I get over this hump and start having fun with ruby.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔梦 2025-01-02 00:41:42

当需要文件时,该文件必须位于名为 $LOAD_PATH 的目录列表中。当前目录曾经在此列表中,但从 1.9.2 开始,出于安全原因已将其删除。

您有四个选项(按我认为的好坏顺序列出)

1 将您的目录结构更改为如下所示

.
|-- lib
|   `-- bowling.rb
`-- spec
    `-- bowling_spec.rb

然后以 rspec spec 而不是 ruby Bowling_spec.rb 运行>
这是有效的,因为 RSpec 会看到 lib 位于您的当前目录中,然后将其添加到 $LOAD_PATH 中。

如果您这样做,您也不必需要“rspec”

2 使用 ruby​​ -I 运行。 Bowling_spec.rb

会将当前目录添加到 $LOAD_PATH

3 使用 require_relative 'bowling' 而不是 require 'bowling'

这将查找相对于当前正在运行的文件 (bowling_spec.rb) 的 Bowling.rb 文件

4 使用 require('../bowling', __FILE__) 而不是 require 'bowling'

这个和上面基本是一样的。

其他问题:

问:当 Bowling.rb 和 Bowling_spec.rb 位于同一文件夹中时,为什么我会收到 LoadError?

答:因为当前目录(运行脚本的目录,而不是文件所在的目录)不在$LOAD_PATH 中。

问:在错误中,当我运行 ruby​​ 1.9.2 时,到底是 .../site_ruby/1.9.1/... ,为什么会出现 1.9.1 呢?

答:嗯。不确定我记得是否准确,但是 IIRC,它们非常相似,以至于界面没有改变,所以从系统角度来看它们可以与 1.9.1 兼容。

问:我该如何克服这个困难并开始享受 ruby​​ 带来的乐趣。

答:我想这要看情况。如果问题是您希望能够运行 CWD 中的文件,那么您可以将环境变量 RUBYLIB 添加到 . 到您的 .bash_profile(或系统上的任何等效项)这将告诉 Ruby 在当前目录中查找文件。不过,这很容易出现错误(并且可能导致无意执行 Ruby 文件,这是一个安全风险)。如果您只是指“我如何开始学习”或者什么是有趣的项目?然后查看我的一个项目,Ruby Kickstart,该项目在六个会话中将带您完成相当大的一部分Ruby,并让您在最后编写并部署一个简单的 Web 应用程序。

When requiring files, the file must be in a list of directories called the $LOAD_PATH. The current directory used to be in this list, but as of 1.9.2, it has been removed for security reasons.

You have four options (listed in order of how good I think they are)

1 Change your directory structure to look like this

.
|-- lib
|   `-- bowling.rb
`-- spec
    `-- bowling_spec.rb

And then run as rspec spec instead of ruby bowling_spec.rb
This works because RSpec will see that lib is in your current directory, and then add it to the $LOAD_PATH for you.

If you do this, you also don't have to require 'rspec'.

2 Run with ruby -I . bowling_spec.rb

which will add the current directory to the $LOAD_PATH

3 Use require_relative 'bowling' instead of require 'bowling'.

This will look for the bowling.rb file relative to the current file being run (bowling_spec.rb)

4 Use require('../bowling', __FILE__) instead of require 'bowling'

This is basically the same as the above.

Other questions:

Q: Why am I getting a LoadError when bowling.rb and bowling_spec.rb are in the same folder?

A: Because the current directory (the directory you are running the script from, not the directory the files are located in) is not in the $LOAD_PATH.

Q: In the error what the heck is .../site_ruby/1.9.1/... when I am running ruby 1.9.2 then why would 1.9.1 even show up?

A: Hmm. Not sure I remember exactly, but IIRC, it was something like they're so similar that the interface hadn't changed, so they could be compatible with 1.9.1 from a system perspective.

Q: how do I get over this hump and start having fun with ruby.

A: I suppose that depends. If the issue is that you want to be able to run files that are in your CWD, then you can add the environment variable RUBYLIB to . to your .bash_profile (or whatever the equivalent is on your system) which will tell Ruby to look in the current directory for files. This is prone to bugs, though (and it could lead to unintentional execution of Ruby files, which is a security risk). If you just mean "how do I start learning" or whats a fun project? Then check out one of my projects, Ruby Kickstart which, in six sessions, will take you through a pretty big portion of Ruby, and have you write and deploy a simple web app by the end of it.

瀞厅☆埖开 2025-01-02 00:41:42

当您require一个文件并且没有指定该文件的绝对路径时,Ruby 会查找其加载路径(在 ruby​​ 中通过 $LOAD_PATH$ 访问:)并检查每个目录中是否有您想要的文件。您无法加载 bowling.rb,因为它不在加载路径的目录中。

解决方案是以下两件事之一:

  1. 将当前目录放在加载路径上:

    $:.unshift File.expand_path('.')
    

    这会将当前工作目录的完整路径放在加载路径上。

  2. 使用 require 和文件的绝对路径:

    require File.expand_path('../保龄球', __FILE__)
    

一些附加信息:File.expand_path 返回当前工作目录中第一个参数的绝对路径,除非有第二个参数给予;然后它使用它作为起点。所以整行可以读作:

require /home/mike/src/something/bowling_spec.rb/../bowling

When you require a file and don't specify an absolutely path to the file, Ruby looks on its load path (accessed within ruby as $LOAD_PATH or $:) and checks each directory there for the file you want. You cannot load bowling.rb because it's not in a directory on your load path.

The solution is one of two things:

  1. Put the current directory on the load path:

    $:.unshift File.expand_path('.')
    

    This puts the full path to the current working directory on the load path.

  2. Use require with the absolute path to the file:

    require File.expand_path('../bowling', __FILE__)
    

A little additional info: File.expand_path returns the absolute path to the first parameter from the current working directory, unless a second parameter is given; then it uses that as the starting point. So the whole line could be read:

require /home/mike/src/something/bowling_spec.rb/../bowling
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文