# Weird problem with DATA and \_\_END\_\_

**URL:** <https://www.ruby-forum.com/t/weird-problem-with-data-and-end/113475>\
**Category:** Ruby\
**Created:** [August 31, 2007, 1:32pm UTC](https://www.ruby-forum.com/t/weird-problem-with-data-and-end/113475 "2007-08-31T13:32:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_TSRichter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@Michael\_TSRichter](https://www.ruby-forum.com/u/Michael_TSRichter)\
**Post date:** [August 31, 2007, 1:32pm UTC](https://www.ruby-forum.com/t/weird-problem-with-data-and-end/113475/1 "2007-08-31T13:32:27Z")

</div>

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?
```

---

<div class="post-metadata">

**Author:** ![Michael\_TSRichter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@Michael\_TSRichter](https://www.ruby-forum.com/u/Michael_TSRichter)\
**Post date:** [August 31, 2007, 1:41pm UTC](https://www.ruby-forum.com/t/weird-problem-with-data-and-end/113475/2 "2007-08-31T13:41:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_TSRichter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@Michael\_TSRichter](https://www.ruby-forum.com/u/Michael_TSRichter)\
**Post date:** [August 31, 2007, 3:50pm UTC](https://www.ruby-forum.com/t/weird-problem-with-data-and-end/113475/3 "2007-08-31T15:50:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_TSRichter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@Michael\_TSRichter](https://www.ruby-forum.com/u/Michael_TSRichter)\
**Post date:** [August 31, 2007, 4:19pm UTC](https://www.ruby-forum.com/t/weird-problem-with-data-and-end/113475/4 "2007-08-31T16:19:57Z")

</div>

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”?

---

<div class="post-metadata">

**Author:** ![Michael\_TSRichter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@Michael\_TSRichter](https://www.ruby-forum.com/u/Michael_TSRichter)\
**Post date:** [August 31, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/weird-problem-with-data-and-end/113475/5 "2007-08-31T16:49:00Z")

</div>

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
