This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
The Marshal class in Ruby Core allows you to convert Ruby objects in to a data stream and back again.
Links
Code Samples
Here is our player class with two attributes: name and progress:
class Player
attr_accessor :name, :progress
def initialize(name)
@name = name
end
end
Using the Marshal library, we can output this player object to a string:
player = Player.new("Jason")
player.progress = 60
player_out = Marshal.dump(player)
puts player_out.inspect
Results in the following:
"\x04\bo:\vPlayer\a:\n@nameI\"\nJason\x06:\x06ET:\x0E@progressiA"
We can then load it later and the object will have the same data:
player_in = Marshal.load(player_out)
puts player_in.inspect
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up