Saving and restoring with YAML

Discussion in 'Ruby' started by Ben Giddings, Oct 1, 2003.

  1. Ben Giddings

    Ben Giddings Guest

    Hi all,

    I have a quick question about YAML. From the documentation it seems as
    though it was designed for, or is well suited to saving/restoring an
    object's state, but from looking at it I can't quite tell what the ideal
    way of doing this is. Say I have a class that looks like:

    class Foo
    attr_accessor :bar, :arr, :buzz

    def initialize
    @bar = true
    @arr = []
    @buzz = "Here is some string"
    end
    end


    What would the "save" and "load" methods of that class look like if I
    wanted to use YAML?

    Ben
    Ben Giddings, Oct 1, 2003
    #1
    1. Advertising

  2. Ben Giddings wrote:
    > Hi all,
    >
    > I have a quick question about YAML. From the documentation it seems as
    > though it was designed for, or is well suited to saving/restoring an
    > object's state, but from looking at it I can't quite tell what the ideal
    > way of doing this is. Say I have a class that looks like:
    >
    > class Foo
    > attr_accessor :bar, :arr, :buzz
    >
    > def initialize
    > @bar = true
    > @arr = []
    > @buzz = "Here is some string"
    > end
    > end
    >
    >
    > What would the "save" and "load" methods of that class look like if I
    > wanted to use YAML?


    If you require 'yaml', your class already has #to_yaml, which is a save
    method, and you can use YAML.load to get it back:

    require 'yaml'
    foo = Foo.new
    YAML.load(foo.to_yaml)

    Maybe I'm misunderstanding your question. Do you mean you want instances
    of Foo to always be serialized in YAML format, even when using Marshal?
    Joel VanderWerf, Oct 1, 2003
    #2
    1. Advertising

  3. Ben Giddings

    Ben Giddings Guest

    Joel VanderWerf wrote:
    > If you require 'yaml', your class already has #to_yaml, which is a save
    > method, and you can use YAML.load to get it back:


    Ah, ok. I didn't realize that kind of magic happened. Cool. But to_yaml
    isn't a "save" method, it's a "to-string" type method, right? So should my
    save method be something like

    def save
    File.open("foo_save.yaml", "w") {|file| file.puts(self.to_yaml) }
    end

    and load like:

    def Foo.load
    File.open("foo_save.yaml", "r") {
    |file|
    YAML.load(file)
    }
    end

    If this is the way to do it, that's great. It would be nice, however, if
    a) it were more symmetrical so you could have a YAML.save(file) as well as
    a YAML.load(file)
    b) YAML.load (and YAML.save) could do the File opening, writing, and
    closing as well.

    Ben
    Ben Giddings, Oct 1, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    329
    Steve C. Orr [MVP, MCSD]
    Jan 12, 2004
  2. Jacob H
    Replies:
    4
    Views:
    367
    M.E.Farmer
    Jan 25, 2005
  3. Marcus Kwok
    Replies:
    15
    Views:
    688
    Marcus Kwok
    Feb 20, 2006
  4. Wolfgang Thomsen

    Saving and restoring streampos's

    Wolfgang Thomsen, Apr 25, 2007, in forum: C++
    Replies:
    1
    Views:
    381
    James Kanze
    Apr 26, 2007
  5. Andreas Drop

    Saving and Restoring a GTK:TreeModel

    Andreas Drop, Apr 10, 2010, in forum: Ruby
    Replies:
    0
    Views:
    97
    Andreas Drop
    Apr 10, 2010
Loading...

Share This Page