How to marshal a Ruby object

Posted by Daniel Vela on March 22, 2015 · 1 min read

To serialize and to restore objects in Ruby use Marshal class.

Marshal class documentation

class Klass
  def initialize(str)
    @str = str
  end
  def say_hello
    @str
  end
end


o = Klass.new("hello\n")
data = Marshal.dump(o)
obj = Marshal.load(data)
obj.say_hello  #=> "hello\n"