Back to Our Regularly Scheduled Programming

Now where were we? Ah, yes, YAML. As I was saying, YAML takes (or returns) a multiline string. Go ahead and play around with your YAML code. Get familiar with it. Toss in some arrays within arrays; try to fool it with the integer 42 as opposed to the string '42' or with the true object as opposed to the string 'true'. YAML is pretty smart and, if I may be so bold, darned convenient.

You know what would be even better, though? It would be cool if I could just save an object with one method call, just one line of code. And it’d be cool if I could load with just one method call, too. Check it, yo!

require ​'yaml'
# First we define these fancy methods...
def​ yaml_save object, filename
File.open filename, ...

Get Learn to Program, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.