Writing a custom YAML emitter

Discussion in 'Ruby' started by Ben Giddings, Jul 28, 2004.

  1. Ben Giddings

    Ben Giddings Guest

    Hi all,

    I'm trying to understand how to write a custom YAML emitter. As fun
    and interesting as Why's documentation is, I don't quite get how to do
    what I need.

    What I'm looking for is a way of avoiding emitting certain instance
    variables. Say I have a class like this:

    class Book
    attr_accessor :title, :chapter_titles
    attr_accessor :current_page
    end

    When I save it, I want to save the permanent features like "title" and
    "chapter_titles", but I want to forget the current page.

    I think the YAML representation would be something like:

    --- !ruby/object:Bar
    title: Fun with YAML
    chapter_titles:
    - your first yaml document
    - daily yaml chores
    - advanced yaml fun

    From looking at "rubytypes.rb", it looks like if I redefine
    "to_yaml_properties" to return ["@title", "@chapter_titles"] it should
    work. But is this the right way? Is there another preferred way?

    Ben
     
    Ben Giddings, Jul 28, 2004
    #1
    1. Advertising

  2. Ben Giddings wrote:
    >
    > From looking at "rubytypes.rb", it looks like if I redefine
    > "to_yaml_properties" to return ["@title", "@chapter_titles"] it should
    > work. But is this the right way? Is there another preferred way?
    >


    That's it. The to_yaml_properties is generally what I use 90% of the time.

    If you decide you want to write a full-blown to_yaml method, I'd look at
    the to_yaml methods in rubytypes.rb to get an idea of how it's done, as
    the deep-down-to-the-black-black-sea-floor API isn't documentated at all.

    _why
     
    why the lucky stiff, Jul 28, 2004
    #2
    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. Kirill Simonov
    Replies:
    0
    Views:
    359
    Kirill Simonov
    May 8, 2006
  2. Kirill Simonov
    Replies:
    1
    Views:
    342
    Scott David Daniels
    May 15, 2006
  3. Kirill Simonov
    Replies:
    0
    Views:
    261
    Kirill Simonov
    Jun 19, 2006
  4. Kirill Simonov
    Replies:
    0
    Views:
    299
    Kirill Simonov
    Aug 20, 2006
  5. Kirill Simonov
    Replies:
    0
    Views:
    335
    Kirill Simonov
    May 12, 2007
Loading...

Share This Page