Gzip'd Marshal

People complain that Marshal’s Binary output can’t be read by humans, and everyone should use either YAML (slow) or JSON (limited) when dumping. The whole idea behind this “argument”, is that humans can read in the first place! Honestly, when’s the last time you picked up a book?

Anyways, binary is great for those of us that can’t read to begin with, and compressed binary is even better! 

thank this guy for the code you’re about to steal:

The idea is simple:

Writing: Object -> Marshal -> compress

Reading: Decompress -> Marshal -> Object

require'zlib'

# Save any ruby object to disk!
# Objects are stored as gzipped marshal dumps.
#
# Example
#
# # First ruby process
# hash = {1 => "Entry1", 2 => "Entry2"}
# ObjectStash.store hash, 'hash.stash'
#
# ...
#
# # Another ruby process - same_hash will be identical to the original hash
# same_hash = ObjectStash.load 'hash.stash'
class ObjectStash

# Store an object as a gzipped file to disk
#
# Example
#
# hash = {1 => "Entry1", 2 => "Entry2"}
# ObjectStore.store hash, 'hash.stash.gz'
# ObjectStore.store hash, 'hash.stash', :gzip => false
def self.storeobj,file_name,options={}
marshal_dump=Marshal.dump(obj)
file=File.new(file_name,'w')
file=Zlib::GzipWriter.new(file)unlessoptions[:gzip]==false
file.writemarshal_dump
file.close
returnobj
end

# Read a marshal dump from file and load it as an object
#
# Example
#
# hash = ObjectStore.get 'hash.dump.gz'
# hash_no_gzip = ObjectStore.get 'hash.dump.gz'
def self.loadfile_name
begin
file=Zlib::GzipReader.open(file_name)
rescueZlib::GzipFile::Error
file=File.open(file_name,'r')
ensure
obj=Marshal.loadfile.read
file.close
returnobj
end
end
end

if$0==__FILE__
require'test/unit'
class TestObjectStash<Test::Unit::TestCase
@@tmp='/tmp/TestObjectStash.stash'
def test_hash_store_load
hash1={:test=>'test'}
ObjectStash.storehash1,@@tmp
hash2=ObjectStash.load@@tmp
asserthash1==hash2
end
def test_hash_store_load_no_gzip
hash1={:test=>'test'}
ObjectStash.storehash1,@@tmp,:gzip=>false
hash2=ObjectStash.load@@tmp
asserthash1==hash2
end
def test_self_stash
ObjectStash.storeObjectStash,@@tmp
assertObjectStash==ObjectStash.load(@@tmp)
end
def test_self_stash_no_gzip
ObjectStash.storeObjectStash,@@tmp,:gzip=>false
assertObjectStash==ObjectStash.load(@@tmp)
end
end
end

Size differences: non-compressed (txt) vs. compressed