Weird problem with DATA and __END__

File: test1.rb:

    require 'test2'
    puts "test1.rb = " + DATA.readlines.to_s
    __END__
    This is the data in test1.rb.

File: test2.rb:

    puts "test2.rb = " + DATA.readlines.to_s
    __END__
    This is the data in test2.rb.

Command session:

    $ ruby test1.rb
    test2.rb = This is the data in test1.rb.
    test1.rb =
    $

To say this is unexpected is putting it mildly. What I’d expect to see
is:

    $ ruby test1.rb
    test2.rb = This is the data in test1.rb.
    test1.rb =
    $

So two questions:

  * Why is test2 getting the data from test1 and not from itself?
  * How can get the behaviour I'm looking for?

On Fri, 2007-31-08 at 20:31 +0900, Michael T. Richter wrote:

To say this is unexpected is putting it mildly. What I’d expect to
see is:

    $ ruby test1.rb
    test2.rb = This is the data in test1.rb.
    test1.rb = 
    $ 

sigh Of course what I really meant was I expected this:

    test2.rb = This is the data in test2.rb.
    test1.rb = This is the data in test1.rb

That’ll teach me to post before proof-reading.

Hi Michael,

  1. DATA constant becomes defined:
    DATA.inspect # => “This is the data in test1.rb.”
require 'test2'

snip
2. test2 becomes loaded
3. test2 reads lines from DATA(0) to DATA(EOF) and puts them

puts "test2.rb = " + DATA.readlines.to_s

test2.rb = This is the data in test1.rb.

ignored unless it’s called first

__END__
This is the data in test2.rb.

snip
4. test1 reads lines from DATA(EOF) to DATA(EOF) and puts them

puts "test1.rb = " + DATA.readlines.to_s

test1.rb =

__END__
This is the data in test1.rb.

Your dealing with a constant here, you should expect that it won’t
change. Ok,
you maybe expected that the require behaves like the ruby-bin…

Regards Florian

On Fri, 2007-31-08 at 22:48 +0900, Florian Aßmann wrote:

test2.rb = This is the data in test1.rb.

__END__
This is the data in test1.rb.

Your dealing with a constant here, you should expect that it won’t change. Ok,
you maybe expected that the require behaves like the ruby-bin…

OK, that makes a twisted sort of sense, but… I REALLY do not like
this. At all. Not just because it royally screws what I’m working on
(although I may have a slightly clumsier workaround) but also because it
means any libraries I require in my code can suck up my END data. I
would have expected the DATA constant to be localized in some way to the
language unit it’s being used in, not passed around freely like some
kind of depraved party girl.

And what’s the “ruby-bin”?

Hi Michael,

I once met this problem, too - but you know - you can always do for each
file
you require:

module FileA
DATA = File.read FILE.gsub(/.rb$/, ‘.txt’)
end

Just put each const in it’s own Namespace/Module and lessen your usage
of
END. I always work in my own Namespaces, but not for each require
though…

OK, that makes a twisted sort of sense, but… I REALLY do not like
this. At all. Not just because it royally screws what I’m working on
(although I may have a slightly clumsier workaround) but also because it
means any libraries I require in my code can suck up my END data. I
would have expected the DATA constant to be localized in some way to the
language unit it’s being used in, not passed around freely like some
kind of depraved party girl.
rofl

And what’s the “ruby-bin”?
Naah, I mean the executable…

Regards
Florian