Heads up: This description was created by AI and might not be 100% accurate.
serialization.rb
This Ruby code snippet demonstrates object serialization using Marshal.dump
to convert a Ruby hash (person
) into a byte string. This byte string is then written to a binary file named person.dat
in the input/serialization
directory. This process persists the Ruby object’s data to a file for later retrieval and deserialization.
Ruby code snippet
person = { name: "Alice", age: 30 }
#=> {name: "Alice", age: 30}
serialized_person = Marshal.dump(person)
#=> "\x04\b{\a:\tnameI\"\nAlice\x06:\x06ET:\bagei#"
File.open('input/serialization/person.dat', 'wb') do |file|
file.write(serialized_person)
end
#=> 30
Executed with Ruby 3.4.5
.