Heads up: This description was created by AI and might not be 100% accurate.
serialization.rb
This Ruby code snippet demonstrates serializing an object into a binary format using the Marshal class. The Marshal
module provides methods for converting objects to and from binary data, which can be stored in files or transmitted over network connections.
The code first defines a hash with two key-value pairs, where each key is a string representing the name of an object property (in this case, “name” and “age”) and each value is the corresponding property value (in this case, the strings “Alice” and “30”).
Next, the code uses the Marshal.dump
method to convert the hash into a binary representation, which is stored in the variable serialized_person
.
Finally, the code opens a file named ‘input/serialization/person.dat’ in write-binary mode using the File.open
method and writes the serialized data to it using the file.write
method.
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.4
.