Emit YAML without leading hyphens?

Discussion in 'Ruby' started by bwv549, Apr 7, 2009.

  1. bwv549

    bwv549 Guest

    How to emit yaml without the leading hyphens?

    [1,2,3].to_yaml
    # ==>"--- \n- 1\n- 2\n- 3\n"

    I realize that I can do the following to get the result I'm after, but
    it seems sort of "after the fact"
    [1,2,3].to_yaml[5..-1]
    # ==>"- 1\n- 2\n- 3\n"

    I know 'to_yaml' takes options, but I can't find acceptable options
    anywhere.
    [1,2,3].to_yaml({:??? => true})


    Thanks!
     
    bwv549, Apr 7, 2009
    #1
    1. Advertising

  2. bwv549

    Ryan Davis Guest

    On Apr 7, 2009, at 14:14 , bwv549 wrote:

    > How to emit yaml without the leading hyphens?
    >
    > [1,2,3].to_yaml
    > # ==>"--- \n- 1\n- 2\n- 3\n"
    >
    > I realize that I can do the following to get the result I'm after, but
    > it seems sort of "after the fact"
    > [1,2,3].to_yaml[5..-1]
    > # ==>"- 1\n- 2\n- 3\n"
    >
    > I know 'to_yaml' takes options, but I can't find acceptable options
    > anywhere.
    > [1,2,3].to_yaml({:??? => true})



    As I understand it, if you remove the hyphens, it is no longer yaml,
    so don't do that.
     
    Ryan Davis, Apr 7, 2009
    #2
    1. Advertising

  3. Ryan Davis wrote:
    >
    > On Apr 7, 2009, at 14:14 , bwv549 wrote:
    >
    >> How to emit yaml without the leading hyphens?
    >>
    >> [1,2,3].to_yaml
    >> # ==>"--- \n- 1\n- 2\n- 3\n"
    >>
    >> I realize that I can do the following to get the result I'm after, but
    >> it seems sort of "after the fact"
    >> [1,2,3].to_yaml[5..-1]
    >> # ==>"- 1\n- 2\n- 3\n"
    >>
    >> I know 'to_yaml' takes options, but I can't find acceptable options
    >> anywhere.
    >> [1,2,3].to_yaml({:??? => true})

    >
    >
    > As I understand it, if you remove the hyphens, it is no longer yaml, so
    > don't do that.


    Maybe so, but the yaml extension loads it anyway.

    irb(main):001:0> [1,2,3].to_yaml[5..-1]
    => "- 1\n- 2\n- 3\n"
    irb(main):002:0> YAML.load(_)
    => [1, 2, 3]

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Apr 7, 2009
    #3
  4. bwv549

    Henrik Hodne Guest

    Just a quick question: Why do you want to do this?

    Regards,
    Henrik Hodne

    On 7 Apr 2009, at 23:14, bwv549 <> wrote:

    > How to emit yaml without the leading hyphens?
    >
    > [1,2,3].to_yaml
    > # ==>"--- \n- 1\n- 2\n- 3\n"
    >
    > I realize that I can do the following to get the result I'm after, but
    > it seems sort of "after the fact"
    > [1,2,3].to_yaml[5..-1]
    > # ==>"- 1\n- 2\n- 3\n"
    >
    > I know 'to_yaml' takes options, but I can't find acceptable options
    > anywhere.
    > [1,2,3].to_yaml({:??? => true})
    >
    >
    > Thanks!
    >
     
    Henrik Hodne, Apr 8, 2009
    #4
  5. bwv549

    Alex Young Guest

    Henrik Hodne wrote:
    > Just a quick question: Why do you want to do this?
    >


    Can't speak for bwv549, but when I've wanted to do this in the past it's
    so that I can emit part of an array to the end of an existing YAML file
    such that it loads as a single YAML document at a later stage.

    --
    Alex
    --
    Posted via http://www.ruby-forum.com/.
     
    Alex Young, Apr 8, 2009
    #5
  6. bwv549

    bwv549 Guest

    > Can't speak for bwv549, but when I've wanted to do this in the past it's
    > so that I can emit part of an array to the end of an existingYAMLfile
    > such that it loads as a singleYAMLdocument at a later stage.


    exactly the situation.
     
    bwv549, Apr 9, 2009
    #6
  7. On Thu, Apr 9, 2009 at 11:00 PM, bwv549 <> wrote:
    >> Can't speak for bwv549, but when I've wanted to do this in the past it's
    >> so that I can emit part of an array to the end of an existingYAMLfile
    >> such that it loads as a singleYAMLdocument at a later stage.

    >
    > exactly the situation.
    >


    I would stick with the "after the fact" changes to the YAML output as
    tacking on YAML from one array onto the output of a different YAML
    file is a very specific use-case.

    Andrew Timberlake
    http://ramblingsonrails.com
    http://www.linkedin.com/in/andrewtimberlake

    "I have never let my schooling interfere with my education" - Mark Twain
     
    Andrew Timberlake, Apr 10, 2009
    #7
  8. bwv549

    Marc Heiler Guest

    My problem with those hyphens is that a yaml file without hyphens is
    perfectly valid yaml (ruby yaml loads it happily) - i simply dont
    understand why those hyphens are created, when the yaml file _without_
    hyphens works perfectly nice.
    --
    Posted via http://www.ruby-forum.com/.
     
    Marc Heiler, Apr 10, 2009
    #8
  9. bwv549

    Simon Chiang Guest


    > perfectly valid yaml (ruby yaml loads it happily) - i simply dont
    > understand why those hyphens are created, when the yaml file _without_
    > hyphens works perfectly nice.


    One reason for the hyphens is to allow multiple YAML documents (ie
    separate objects) to be stored in a single file:

    docs = [1,2,3].to_yaml + [4,5,6].to_yaml

    results = []
    YAML.load_documents(docs) do |doc|
    results << doc
    end
    puts results.inspect # => "[[1,2,3], [4,5,6]]"

    Notice each of the arrays is loaded separately. As for removing the
    header, take a look at this post:

    http://www.arkanis-development.de/weblog/2007/6/20/options-for-rubys-@to_yaml@-method

    The author was facing the same problem. The option is {:UseHeader =>
    false} but the to_yaml options aren't currently used and don't work,
    even though at some point they probably did. It's a deficiency in the
    current release.
     
    Simon Chiang, Apr 10, 2009
    #9
    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. John Nagle
    Replies:
    4
    Views:
    612
    James Antill
    Aug 7, 2007
  2. Delaney, Timothy (Tim)
    Replies:
    0
    Views:
    579
    Delaney, Timothy (Tim)
    Aug 7, 2007
  3. Austin Ziegler

    1.8.1 YAML emit bug? (Win32)

    Austin Ziegler, May 17, 2004, in forum: Ruby
    Replies:
    1
    Views:
    112
    why the lucky stiff
    May 17, 2004
  4. Daniel Sheppard
    Replies:
    3
    Views:
    157
    Caleb Clausen
    Aug 22, 2010
  5. Michelle
    Replies:
    1
    Views:
    82
Loading...

Share This Page