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
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. <
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”
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
Err, I guess DATA comes through as a File object, not a String, but you
get
the idea…
On 5/25/10, Tony A. [email protected] 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.
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.