# In-code data

**URL:** <https://www.ruby-forum.com/t/in-code-data/189173>\
**Category:** Ruby\
**Created:** [May 26, 2010, 12:25am UTC](https://www.ruby-forum.com/t/in-code-data/189173 "2010-05-26T00:25:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeromeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8edcca/32.png) [@jeromeqc](https://www.ruby-forum.com/u/jeromeqc)\
**Post date:** [May 26, 2010, 12:25am UTC](https://www.ruby-forum.com/t/in-code-data/189173/1 "2010-05-26T00:25:57Z")

</div>

Hi,

What is the best way/practice of storing data that is to be used and  
accessed within your code. For example if I downloded a language  
dictionay in txt format. Would it make more sense to read it from a text  
file and parse it into a hash table variable. Or could I have it  
contained within a secondary source file and simple use ‘require’ to  
load it. I work with a lot of specifications which often contain lots of  
tables with values, I would like to simply cut and paste the tables into  
my code. This would make the code easier to update between versions of  
specs especially where the data in the tables has changed. I know this  
sounds like a general question, but I am all ears to any ideas of new  
ways of working.

TC

---

<div class="post-metadata">

**Author:** ![jeromeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8edcca/32.png) [@jeromeqc](https://www.ruby-forum.com/u/jeromeqc)\
**Post date:** [May 26, 2010, 2:42am UTC](https://www.ruby-forum.com/t/in-code-data/189173/2 "2010-05-26T02:42:53Z")

</div>

If you’d like to include some data inline with your code, you can do it  
like  
this:

puts DATA

**END**  
Look at me, I’m some data!

The DATA constant will be populated with whatever text remains after  
**END**

On Tue, May 25, 2010 at 4:26 PM, Jerome David S. \<

---

<div class="post-metadata">

**Author:** ![jeromeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8edcca/32.png) [@jeromeqc](https://www.ruby-forum.com/u/jeromeqc)\
**Post date:** [May 26, 2010, 3:21am UTC](https://www.ruby-forum.com/t/in-code-data/189173/3 "2010-05-26T03:21:31Z")

</div>

Jerome David S. wrote:

> specs especially where the data in the tables has changed. I know this  
> sounds like a general question, but I am all ears to any ideas of new  
> ways of working.
> 
> TC

Since you are updating the data periodically, you might want to consider  
setting up some rake tasks to download and parse the data and store it  
in a binary format that can be loaded quickly. This would probably be  
faster (for you) than cut and paste and faster (for the computer) than  
parsing on each run.

file “download.txt” do

# code to download file

sh “wget [http://vii.path.berkeley.edu/~vjoel/download.txt](http://vii.path.berkeley.edu/~vjoel/download.txt)”  
end

rule “.dat” =\> “.txt” do |t|  
h = {}

File.open(t.prerequisites[0]) do |f|  
f.each do |line|  
key, value = line.split  
h[key] = value  
end  
end

File.open(t.name, “wb”) do |f|  
Marshal.dump(h,f)  
end  
end

task :clean do  
rm “download.txt”  
rm “download.dat”  
end

task :run =\> “download.dat” do  
h = File.open(“download.dat”, “rb”) do |f|  
Marshal.load(f)  
end  
p h  
end

---

<div class="post-metadata">

**Author:** ![jeromeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8edcca/32.png) [@jeromeqc](https://www.ruby-forum.com/u/jeromeqc)\
**Post date:** [May 26, 2010, 2:44am UTC](https://www.ruby-forum.com/t/in-code-data/189173/4 "2010-05-26T02:44:03Z")

</div>

Err, I guess DATA comes through as a File object, not a String, but you  
get  
the idea…

---

<div class="post-metadata">

**Author:** ![jeromeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8edcca/32.png) [@jeromeqc](https://www.ruby-forum.com/u/jeromeqc)\
**Post date:** [May 26, 2010, 7:30pm UTC](https://www.ruby-forum.com/t/in-code-data/189173/5 "2010-05-26T19:30:54Z")

</div>

On 5/25/10, Tony A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If you’d like to include some data inline with your code, you can do it like  
> this:
> 
> puts DATA
> 
> **END**  
> Look at me, I’m some data!
> 
> The DATA constant will be populated with whatever text remains after **END**

However, you should be aware that this only works for the ‘main’ file  
(which one was mentioned on the ruby command line). Other files which  
are pulled in via require or load will not be able to get at trailing  
data via **END** /DATA.

---

<div class="post-metadata">

**Author:** ![jeromeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8edcca/32.png) [@jeromeqc](https://www.ruby-forum.com/u/jeromeqc)\
**Post date:** [May 26, 2010, 3:25am UTC](https://www.ruby-forum.com/t/in-code-data/189173/6 "2010-05-26T03:25:37Z")

</div>

Joel VanderWerf wrote:

> Since you are updating the data periodically, you might want to consider  
> setting up some rake tasks to download and parse the data and store it  
> in a binary format that can be loaded quickly. This would probably be  
> faster (for you) than cut and paste and faster (for the computer) than  
> parsing on each run.

Addendum: that code should be saved in a file called “rakefile”, and  
you’ll need to “gem install rake”. Then you can do “rake run” to  
download, parse, cache the hash table as a binary file, and read it to  
do some work.
