Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
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
Let's say that you are writing
a text based game and
0:00
wanted to save the current
players progress.
0:03
How would you do it?
0:06
You would need to take into
account the different things.
0:08
You could have to save the player,
any items it has, and so on.
0:11
One thing that you can do
to save the game state
0:16
is use the Marshal class
to save Ruby objects.
0:18
Marshal class in Ruby core,
0:23
allows you to convert Ruby objects
into a data stream and back again.
0:25
Let's go ahead a take a look
at that now using work spaces.
0:31
Now here's the documentation for
the Marshal class.
0:35
Now it says here the Marshalling library
converts collections of ruby objects into
0:39
a byte stream, allowing them to be stored
outside the currently active script.
0:44
What in the world does this mean?
0:50
Well, this means that you
can dump your current ruby
0:52
objects into a string and
then that string can be loaded later on.
0:57
Let's go ahead and
take a look at this inside of IRB.
1:02
Now I've got this very simple class
that I've written called Player.
1:07
It's got attribute accessors for
name and progress.
1:10
That's gonna be how far along
the player is in the game.
1:14
So I'm gonna go ahead and launch IRB and
require the Player class at the same time.
1:17
We can do this by typing
IRB -r in the path to
1:23
the file that you'd like to require,
-r stands for require.
1:28
So let me go ahead and
create a new player.
1:33
Say a player is Player.new.
1:36
Jason and the player.progress is 40,
only 40% through the game.
1:42
So now, if I wanted to output this player,
1:50
let's go ahead and
head back to the Marshal documentation.
1:54
We could use Marshal.dump and
Marshal.load to load these things.
2:02
So I could say the output is
equivalent to Marshal.dump(player).
2:09
Now if we look at this we
have a string representation.
2:17
Which also contains our variable date.
2:25
Now once we have this out,
we could pass this around, right?
2:29
So here's our string.
2:33
Let's go ahead and load it though and
2:35
say Marshal.load(player) and we'll
assign this to another variable here.
2:37
And we'll just say this new_player object
is equal to loading Marshal.load(output).
2:45
Now if we look at this new_player,
it is another object.
2:54
It's got the same data
as our other player,
2:59
same progress and same name.
3:05
So, those attributes stayed the same.
3:09
Now we could use this for
other object too.
3:11
It's not limited to just
classes that we've written and
3:14
if it gets very complicated,
we can actually go through and
3:18
serialize our data
a little bit differently.
3:22
Now, there are some security
considerations when you
3:26
are using the Marshalling library.
3:28
The load method deserializes
any class loaded into Ruby, so
3:32
you need to make sure
that you trust your data.
3:37
This isn't something that you would
want to be loading straight from, for
3:40
example, a form on a web page.
3:44
Try using Marshall on your
own now using work spaces.
3:47
You need to sign up for Treehouse in order to download course files.
Sign up