Quickly and Easily Storing Data
Author |
Message |
wtd
|
Posted: Sat Mar 26, 2005 11:57 pm Post subject: Quickly and Easily Storing Data |
|
|
The Problem
We have some data in a program and we need to store it in a file so we can get at it later.
Solutions
Well, we could cook up our own solution. In the Turing forums I've seen this so many times it boggles the mind. But the problem with that is that everyone comes up with their own half-baked solution and no one can use anyone else's datafiles in their own programs.
How about a common, standardized data format?
Enter XML. But, there's a problem with XML. It's big. Let's look at a possible representation of an array in XML.
code: | <array>
<item>42</item>
<item>"hello"</item>
</array> |
Considering how little data's in there that's an awful lot of text. XML is highly customizable, and there are times when that's valuable, but it lacks convenient, lightweight and standard syntax for the most common types of data.
So, what are we left with?
Perhaps the most appealing option remaining is YAML. Yet Another Markup Language is just what we're looking for. Let's look at the previous example in YAML.
Or even more simply:
We Have a Winner! Now What?
YAML is not exclusive to Ruby, but has enjoyed a high degree of popularity within the Ruby community due to the excellent support of the YAML library for Ruby.
This library is included standard with Ruby 1.8. If you have a packaged Ruby other than the standard (for instance, the Ubuntu version of Ruby 1.8 is slightly nonstandard) you may need to separately install the YAML library.
Bringing the YAML library into play is a simple matter of including the following lines.
code: | require 'yaml'
include YAML |
Now, to convert a data structure to YAML we need a data structure. Let's try something like the following.
code: | info = [
{
"name" => {"first" => "Bob", "last" => "Smith"},
"car" => {"make" => "AMC", "model" => "Gremlin"}
},
{
"name" => {"first" => "John", "last" => "Doe"},
"car" => {"make" => "Ford", "model" => "Escort"}
}
] |
Now, we can get the equivalent YAML as easily as:
And it looks like:
code: | ---
-
name:
last: Smith
first: Bob
car:
make: AMC
model: Gremlin
-
name:
last: Doe
first: John
car:
make: Ford
model: Escort |
Now, if I want to save this to a file, I need only open a file for writing and print it.
code: | File::open "datafile.dat", "w" do |f|
f.puts info.to_yaml
end |
But, what about getting that information back?
code: | info = YAML::load(File::open("datafile.dat").read) |
Questions?
Any questions? Further information about using YAML that you'd like? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
jamonathin
|
Posted: Thu Mar 31, 2005 9:45 am Post subject: (No subject) |
|
|
wtd, you're crazy man, how do you know all this stuff! Madness i say.
Since regular bits don't mean anything to you . . + Bits. |
|
|
|
|
|
Tony
|
Posted: Thu Mar 31, 2005 12:52 pm Post subject: (No subject) |
|
|
YAML is awesome. It came in really handy in the project I was working on when I read the tutorial. Score.
What I would like to see is a tutorial on the use of hashes. |
|
|
|
|
|
Hikaru79
|
Posted: Sat Apr 02, 2005 8:44 pm Post subject: Re: Quickly and Easily Storing Data |
|
|
wtd wrote:
Perhaps the most appealing option remaining is YAML. Yet Another Markup Language is just what we're looking for. Let's look at the previous example in YAML.
I thought YAML = "YAML Ain't a Markup Language"? (A la "GNU Ain't UNIX") |
|
|
|
|
|
wtd
|
Posted: Sat Apr 02, 2005 8:57 pm Post subject: Re: Quickly and Easily Storing Data |
|
|
Hikaru79 wrote: wtd wrote:
Perhaps the most appealing option remaining is YAML. Yet Another Markup Language is just what we're looking for. Let's look at the previous example in YAML.
I thought YAML = "YAML Ain't a Markup Language"? (A la "GNU Ain't UNIX")
D'oh! |
|
|
|
|
|
Thuged_Out_G
|
Posted: Mon Apr 24, 2006 7:01 pm Post subject: (No subject) |
|
|
Ok, so we have info, which contains all that info. How can you pick it apart, to store the users first name in 'first_name' and so on
or is there a way to do something like:
puts info.first_name |
|
|
|
|
|
Cervantes
|
Posted: Mon Apr 24, 2006 7:25 pm Post subject: (No subject) |
|
|
Well, info is an array. So if we're looking at the first person in the array, we use
or better yet
Now, info.first is a hash. We're interested in names, so we'll do
Now, info.first["name"] is also a hash. We're interested in first names, so we'll do
code: | info.first["name"]["first"] |
This gives us the first name for the first person in the array, info; it gives us "Bob". |
|
|
|
|
|
|
|