Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created January 13, 2011 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/778772 to your computer and use it in GitHub Desktop.
Save JoshCheek/778772 to your computer and use it in GitHub Desktop.
Showing how I would handle some data by using yaml instead of an ad hoc format.
A response for http://www.ruby-forum.com/topic/866786
Just showing how to use a real data format rather than try to create one on the fly, which is fragile and error prone.
Here is a link to yaml docs: http://ruby-doc.org/stdlib/libdoc/yaml/rdoc/index.html
---
- :category: cat1
:items:
- item1
- item2
- item3
- :category: cat2
:items:
- item1
- :category: cat3
:items:
- item1
- item2
- item3
- item4
require 'yaml'
require 'pp'
data = File.open 'data.yml' do |file|
YAML.load file
end
pp data
# this code will get you started so you can see what the data should look like
require 'yaml'
# I'm storing it in an Array, because Hashes are unordered pre 1.9
data = [
{ :category => 'cat1' , :items => ['item1', 'item2', 'item3'] },
{ :category => 'cat2' , :items => ['item1'] },
{ :category => 'cat3' , :items => ['item1', 'item2', 'item3', 'item4'] },
]
File.open 'data.yml' , 'w' do |file|
file.puts YAML.dump(data)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment