From: Jerome David Sallinger on
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
--
Posted via http://www.ruby-forum.com/.

From: Tony Arcieri on
[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 <
imran.nazir(a)yahoo.co.uk> wrote:

> 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
> --
> Posted via http://www.ruby-forum.com/.
>
>


--
Tony Arcieri
Medioh! A Kudelski Brand

From: Tony Arcieri on
[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...

On Tue, May 25, 2010 at 6:42 PM, Tony Arcieri <tony.arcieri(a)medioh.com>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__
>
>
> On Tue, May 25, 2010 at 4:26 PM, Jerome David Sallinger <
> imran.nazir(a)yahoo.co.uk> wrote:
>
>> 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
>> --
>> Posted via http://www.ruby-forum.com/.
>>
>>
>
>
> --
> Tony Arcieri
> Medioh! A Kudelski Brand
>



--
Tony Arcieri
Medioh! A Kudelski Brand

From: Joel VanderWerf on
Jerome David Sallinger wrote:
> 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

From: Joel VanderWerf on
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.