Serialization.rb

This content was produced by an LLM and could include errors.

このスクリプトはMarshalによる永続化(シリアライズ)です。ハッシュ {name: "Alice", age: 30}Marshal.dump でRuby固有のバイナリ形式へ変換し、バイナリモード 'wb' で開いた person.dat へ書き込みます。deserialization.rb と対になるスクリプトです。

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

Ruby 4.0.6