Skip to content

Instantly share code, notes, and snippets.

@miketierney
Created May 6, 2009 22:06
Show Gist options
  • Save miketierney/107777 to your computer and use it in GitHub Desktop.
Save miketierney/107777 to your computer and use it in GitHub Desktop.
require 'yaml'
#=> true
urlhash = { "1.1.1.1" => {:url => "http://www.example.com", :time => Time.now}}
#=> {"1.1.1.1"=>{:time=>Wed May 06 15:03:58 -0700 2009, :url=>"http://www.example.com"}}
open("ip_cache.yml", "w") { |f| YAML.dump(urlhash, f)}
#=> #<File:ip_cache.yml (closed)>
# Generates the following in the YAML file:
#
# ---
# 1.1.1.1:
# :time: 2009-05-06 15:03:58.803427 -07:00
# :url: http://www.example.com
# To access the information inside the YAML file, you can load it in to memory, then
# read out of it just like you would with a hash of hashes.
cached = YAML.load(open("ip_cache.yml"))
#=> {"1.1.1.1"=>{:time=>Wed May 06 15:03:58 -0700 2009, :url=>"http://www.example.com"}}
cached["1.1.1.1"][:url]
#=> "http://www.example.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment