Skip to content

Instantly share code, notes, and snippets.

@jwoertink
Created June 11, 2012 00:52
Show Gist options
  • Select an option

  • Save jwoertink/2907924 to your computer and use it in GitHub Desktop.

Select an option

Save jwoertink/2907924 to your computer and use it in GitHub Desktop.
# Using Ruby 1.9.2p290
require 'msgpack'
require 'base64'
require 'json'
str = "hello" # plain string
bse = Base64.encode64(str) # base64 encoded string
msg = str.to_msgpack # msgpack string
abc = Base64.encode64(msg) # base64 encoded msgpack
jsn = str.to_json # string converted to json
File.open("test1.txt", "w") { |f| f.write(str) } #=> 5bytes
File.open("test2.txt", "w") { |f| f.write(bse) } #=> 9bytes
File.open("test3.txt", "w") { |f| f.write(msg) } #=> 6bytes
File.open("test4.txt", "w") { |f| f.write(abc) } #=> 9bytes
File.open("test5.txt", "w") { |f| f.write(jsn) } #=> 7bytes
# Using a hash
h = {:name => "Jeremy", :age => 30}
File.open("test6.txt", "w") { |f| f.write(h) } #=> 27bytes
File.open("test6.txt", "w") { |f| f.write(h.to_json) } #=> 26bytes
File.open("test7.txt", "w") { |f| f.write(h.to_msgpack) } #=> 18bytes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment