Purpose
This tutorial introduces the basics of using MessagePack Ruby to serialize and deserialize Ruby objects.
References
-
Serialization - Core serialization concepts
-
API reference - Complete API documentation
Installing the gem
Add to your Gemfile:
gem 'messagepack'Or install directly:
gem install messagepackYour first serialization
Basic pack and unpack
require 'messagepack'
# Pack some data
data = { message: "Hello, MessagePack!" }
binary = Messagepack.pack(data)
# Inspect the binary (non-printable characters escaped)
puts binary.inspect
# => "\\x81\\xA7message\\xB4Hello, MessagePack!"
# Unpack the data
result = Messagepack.unpack(binary)
puts result.inspect
# => {"message"=>"Hello, MessagePack!"}Working with different types
# Numbers
Messagepack.pack(42) # Integer
Messagepack.pack(3.14) # Float
Messagepack.pack(-100) # Negative integer
# Strings and symbols
Messagepack.pack("hello") # String
Messagepack.pack(:world) # Symbol (serialized as string)
# Collections
Messagepack.pack([1, 2, 3]) # Array
Messagepack.pack({a: 1}) # Hash
# Special values
Messagepack.pack(nil) # Nil
Messagepack.pack(true) # Boolean true
Messagepack.pack(false) # Boolean falseWorking with IO
Writing to a file
data = { name: "Alice", age: 30, skills: ["Ruby", "Python"] }
# Write to file
File.open("data.msgpack", "wb") do |file|
file.write(Messagepack.pack(data))
endPractical example: Configuration file
Create a simple configuration system using MessagePack:
require 'messagepack'
class Config
def initialize(path)
@path = path
@data = load
end
def [](key)
@data[key]
end
def []=(key, value)
@data[key] = value
save
end
private
def load
if File.exist?(@path)
Messagepack.unpack(File.read(@path, encoding: Encoding::BINARY))
else
{}
end
end
def save
File.write(@path, Messagepack.pack(@data))
end
end
# Usage
config = Config.new("config.msgpack")
config[:database] = { host: "localhost", port: 5432 }
config[:debug] = true
puts config[:database][:host] # => "localhost"Next steps
-
Custom extension types - Learn to serialize custom classes
-
Performance guide - Optimize your serialization code