Aaron Groch, July 28, 2017 | 4 min read

The Marshal’s Here to Save the Data

In my Code Immersion class, I spend a day live-coding a program I call The Bank. The Bank is essentially a program to power an ATM. Users create an account (an Object), and then can view and modify that account. Multiple accounts can be made (and stored in an Array), but they’re only available for as long as the program is running. Once you terminate the program, that data just evaporates into the ether, and when you start the program again, the Array to collect Accounts is reset to zero. It’s a terribly sad situation.


This lets me segway into introducing why Rails is so awesome.


“Ya’ll, once we start using Rails, we won’t have this problem. We’ll have a database, and our data will be saved in tables in there. Then we can turn the server on and off as much as we want; it won’t matter. The data will be there each time we get back.”


And then everyone starts chanting, “Rails, rails, rails!” And champagne bottles are popped. Everyone’s high-fiving everyone else.


Okay, that may be a bit of hyperbole.


Now, just because saving data is nice and easy when using Rails, it doesn’t mean that saving data in a stand-alone Ruby program is a Herculean task.


Ruby has a module that will help you save to a file, and it doesn’t it even need to be required, it’s already loaded. It’s called Marshal (like the law enforcement officer, not Jason Segel’s character from How I Met Your Mother).


Marshal can help you “dump” data into a file (I just use a text file), and also load it back into your program.


Let’s walk through an example using a simplified version of The Bank.


First, we’re going to create a text file called accounts.txt. And in the same directory we’ll create our Ruby program file: simplebank.rb. Remember, we’re going to let Marshal write into accounts.txt, so we won’t touch that at all. All the code will be written in simplebank.rb.


We’ll start by defining an Object class called Account.



So we’re still going to need a place to store Accounts while the program is running. We’ll initialize an array to collect our accounts:




Next, set up a menu – a choice between creating new accounts and view existing accounts:



*Note for PC users: you’ll want to write system “cls” instead of system “clear”


What we’ll do allow the user to continuously create accounts until they’re done. I know, that would seem super-fishy in real life, but it will just make things simpler in terms of this example.


Now we’re at the point where we have a way of creating and collecting accounts while the program is running, but no way to keep those accounts around after a restart of the program. We can open the file we made at beginning (accounts.txt) and use Marshal to dump the array into that file:




There is, however, a problem with this. If we come back and make more accounts, they will override the previous accounts we created. So we need to first check if there are any accounts saved in our text file (File.zero? will do this), and if there are, pull that array from the File using Marshal, add it to the one we just created, and then dump whatever array we now have back into the file (again, using Marshal ).




And one last thing I’m going to add to that method is a clear screen and a redirect back to the menu:




Now, you should be able to create accounts, and go over to accounts.txt and see that there is data in there. Unfortunately, it’s not particularly readable:




So we need to define the “view_accounts” method so that we can pull up those accounts in a readable form in our program. Now, if we just created some accounts, and the program has not been stopped, we don’t need to pull from he file, since we already did that back in “create_accounts” and all we need to do is display what’s in our @accounts array. But if we’re starting the program fresh, and come straight to “view_accounts”, we will need to use Marshal to load the data from our text file.



And at the bottom you’ll see we need to call the “menu” method to get the program to do anything, so don’t forget that!


Now try it out. Create accounts, then view them. Then stop the program and view accounts again to see that they’re still around. Then stop the program again, start again, create more accounts, and view once more – new accounts and saved accounts will all be listed. Amazing!

So there you have it: a simple way to save your Ruby data!