Ruby:我可以要求'一个只包含一个类的文件?

时间:2015-08-05 04:32:47

标签: ruby class rspec module

我的文件/project/lib/invaccessor.rb包含以下内容

class InvAccessor
  def initialize
    @browser = "browser"
  end
end

以及需要它的规范文件project/spec/invaccessor_spec.rb

require_relative '../lib/invaccessor'

describe Invaccessor do
  it {expect(2).to be_even}     
end

当我运行rspec spec/invaccessor.rb时,uninitialized constant error获得了Invaccessor。我是否必须将所有文件内容放在模块中才能访问它们?

我使用的是Ruby 2.2.2。

1 个答案:

答案 0 :(得分:2)

是的,你可以。 在classfile.rb所在的目录中尝试:

__slots__

你绝对不必将课程放入模块中来要求它。