# DATA.readlines in an inner file

**URL:** <https://www.ruby-forum.com/t/data-readlines-in-an-inner-file/55759>\
**Category:** Ruby\
**Created:** [March 23, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/data-readlines-in-an-inner-file/55759 "2006-03-23T17:19:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![agorilla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@agorilla](https://www.ruby-forum.com/u/agorilla)\
**Post date:** [March 23, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/data-readlines-in-an-inner-file/55759/1 "2006-03-23T17:19:19Z")

</div>

Is there any way to use DATA.readlines to read the data (below  
**END** ) in a file that’s been required/included? I want the read  
code to be inside the included/required file.

---

<div class="post-metadata">

**Author:** ![agorilla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@agorilla](https://www.ruby-forum.com/u/agorilla)\
**Post date:** [March 23, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/data-readlines-in-an-inner-file/55759/2 "2006-03-23T18:04:52Z")

</div>

On Fri, 24 Mar 2006, Bill G. wrote:

> Is there any way to use DATA.readlines to read the data (below  
> **END** ) in a file that’s been required/included? I want the read  
> code to be inside the included/required file.
> 
> –  
> Bill G. (aka aGorilla)  
> The best answer to most questions is “it depends”.

i don’t think one can do it directly, but:

```
 harp:~ > cat b.rb
 require 'dynaload'

 module M
   def your_code_here_as_normal
   end
 end

 #
 # add this to the end of the file
 #
 data = open( __FILE__ )
 data.each{|line| break if line =~ /^ __END__ $/}
 Dynaload::export data, "name" => "DATA"

 __END__
 42

 harp:~ > cat a.rb
 require 'dynaload'

 loaded = Dynaload::dynaload "b.rb"

 obj, desc = loaded.objects.select{|obj, desc| desc['name'] == 

```

‘DATA’}.first

```
 puts obj.read

 harp:~ > ruby a.rb
 42

```

regards.

-a

---

<div class="post-metadata">

**Author:** ![agorilla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@agorilla](https://www.ruby-forum.com/u/agorilla)\
**Post date:** [March 23, 2006, 9:05pm UTC](https://www.ruby-forum.com/t/data-readlines-in-an-inner-file/55759/3 "2006-03-23T21:05:53Z")

</div>

On 3/23/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> i don’t think one can do it directly, but:

Thanks much, was hoping there was a more elegant solution. I was just  
going to use it to avoid a ‘here doc’ block. Now, the ‘here doc’  
doesn’t look so bad 😉
