In-code data

  • Thread starter Jerome David Sallinger
  • Start date
J

Jerome David Sallinger

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
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

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 Sallinger <
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

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

Joel VanderWerf

Jerome said:
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

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"
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
 
J

Joel VanderWerf

Joel said:
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.
 
C

Caleb Clausen

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top