RSpec doesn't see the DATA constant

Hi.

In Ruby there’s the DATA constant which contains the lines following
the __END__ keyword in the source file. For some reason RSpec
doesn’t see it.

Here’s the example:

# data_spec.rb

requre 'rspec'

describe 'DATA' do
  it 'contains lines following the __END__ keyword' do
    DATA.read.should == "Hello from underground!\n"
  end
end

__END__
Hello from underground!

If then I run $ rspec data_spec.rb, I’m getting ‘NameError:
uninitialized constant RSpec::Core::ExampleGroup::Nested_1::DATA’.
However, if I use $ ruby data_spec.rb all works fine.

Why RSpec doesn’t see the constant when I use the rspec command? How
can I solve the problem?

Thanks.

Debian GNU/Linux 5.0.7;

Ruby 1.9.2;

RSpec 2.5.0.

P. A. wrote in post #983523:

Hi.

In Ruby there’s the DATA constant which contains the lines following
the __END__ keyword in the source file. For some reason RSpec
doesn’t see it.

Here’s the example:

# data_spec.rb

requre 'rspec'

describe 'DATA' do
  it 'contains lines following the __END__ keyword' do
    DATA.read.should == "Hello from underground!\n"
  end
end

__END__
Hello from underground!

If then I run $ rspec data_spec.rb, I’m getting ‘NameError:
uninitialized constant RSpec::Core::ExampleGroup::Nested_1::DATA’.
However, if I use $ ruby data_spec.rb all works fine.

DATA works only on the first file called by the interpreter.

first.rb:
require ‘second’

second.rb:
p DATA.read

END
BAM!

ruby first.rb
./second.rb:1: uninitialized constant DATA (NameError)
from first.rb:1:in `require’
from first.rb:1

ruby second.rb
“BAM!\n”

Why RSpec doesn’t see the constant when I use the rspec command? How
can I solve the problem?

The restriction on DATA notwithstanding, DATA in that context will by
ambiguous. Think about it, is the DATA from the rspec file or the DATA
from the tested file?

Thanks for the explanations, Costi G. I didn’t thought that the DATA
constant isn’t seen by the interpreter if it is in the required file.

The only solution that I can think of right now is the following:

# data_spec.rb

describe 'DATA' do
  it 'contains lines following the __END__ keyword' do
    data = File.read(__FILE__).split("__END__\n")[-1]
    data.should == "Hello from the underground!\n"
  end
end

__END__
Hello from the underground!

Costi G.:

On 25 Feb 2011, at 00:26, Shamaoke wrote:

Thanks for the explanations, Costi G. I didn’t thought that the DATA
constant isn’t seen by the interpreter if it is in the required file.

The only solution that I can think of right now is the following:

Why not just File.read(DATA_FILE) and keep the data in a separate file?

doesn’t see it.
DATA.read.should == “Hello from underground!\n”


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

cheers,
Matt

[email protected]
07974 430184

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs