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