Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Marshal3:52 with Jason Seifer
The Marshal class in Ruby Core allows you to convert Ruby objects in to a data stream and back again.
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:
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
wanted to save the current players progress.
How would you do it?
You would need to take into account the different things.
You could have to save the player, any items it has, and so on.
One thing that you can do to save the game state
is use the Marshal class to save Ruby objects.
Marshal class in Ruby core,
allows you to convert Ruby objects into a data stream and back again.
Let's go ahead a take a look at that now using work spaces.
Now here's the documentation for the Marshal class.
Now it says here the Marshalling library converts collections of ruby objects into
a byte stream, allowing them to be stored outside the currently active script.
What in the world does this mean?
Well, this means that you can dump your current ruby
objects into a string and then that string can be loaded later on.
Let's go ahead and take a look at this inside of IRB.
Now I've got this very simple class that I've written called Player.
It's got attribute accessors for name and progress.
That's gonna be how far along the player is in the game.
So I'm gonna go ahead and launch IRB and require the Player class at the same time.
We can do this by typing IRB -r in the path to
the file that you'd like to require, -r stands for require.
So let me go ahead and create a new player.
Say a player is Player.new.
Jason and the player.progress is 40, only 40% through the game.
So now, if I wanted to output this player,
let's go ahead and head back to the Marshal documentation.
We could use Marshal.dump and Marshal.load to load these things.
So I could say the output is equivalent to Marshal.dump(player).
Now if we look at this we have a string representation.
Which also contains our variable date.
Now once we have this out, we could pass this around, right?
So here's our string.
Let's go ahead and load it though and
say Marshal.load(player) and we'll assign this to another variable here.
And we'll just say this new_player object is equal to loading Marshal.load(output).
Now if we look at this new_player, it is another object.
It's got the same data as our other player,
same progress and same name.
So, those attributes stayed the same.
Now we could use this for other object too.
It's not limited to just classes that we've written and
if it gets very complicated, we can actually go through and
serialize our data a little bit differently.
Now, there are some security considerations when you
are using the Marshalling library.
The load method deserializes any class loaded into Ruby, so
you need to make sure that you trust your data.
This isn't something that you would want to be loading straight from, for
example, a form on a web page.
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