How to doing a "grep -v" to hide "---" line added by Hash#to_yaml ?

Discussion in 'Ruby' started by Iñaki Baz Castillo, Jun 29, 2008.

  1. Hi, when I convert a Hash to a YAML object and print it a line "---" is add=
    ed=20
    at the beginning:

    =2D--------------------------------------------------------
    cli> require 'yaml'
    cli> puts({1=3D>2,"ABC" =3D> {"a"=3D>"A","b"=3D>"B"}}.to_yaml)
    ---
    ABC:
    a: A
    b: B
    1: 2
    =3D> nil
    =2D--------------------------------------------------------

    Well, how could I hide that annoyin "---" line? Maybe using "grep" in some=
    =20
    way? other suggestion?

    Thanks a lot.


    =2D-=20
    I=C3=B1aki Baz Castillo
     
    Iñaki Baz Castillo, Jun 29, 2008
    #1
    1. Advertising

  2. Re: How to doing a "grep -v" to hide "---" line added byHash#to_yaml ?

    Iñaki Baz Castillo wrote:
    > Hi, when I convert a Hash to a YAML object and print it a line "---" is
    > added
    > at the beginning:
    >
    > ---------------------------------------------------------
    > cli> require 'yaml'
    > cli> puts({1=>2,"ABC" => {"a"=>"A","b"=>"B"}}.to_yaml)
    > ---
    > ABC:
    > a: A
    > b: B
    > 1: 2
    > => nil
    > ---------------------------------------------------------
    >
    > Well, how could I hide that annoyin "---" line? Maybe using "grep" in
    > some
    > way? other suggestion?
    >
    > Thanks a lot.


    Try this

    x = {1=>2,"ABC" => {"a"=>"A","b"=>"B"}}.to_yaml
    x = x[5...x.length]
    --
    Posted via http://www.ruby-forum.com/.
     
    Shashank Agarwal, Jun 29, 2008
    #2
    1. Advertising

  3. Re: How to doing a "grep -v" to hide "---" line added byHash#to_yaml ?

    El Domingo, 29 de Junio de 2008, Shashank Agarwal escribi=C3=B3:
    > Try this
    >
    > x =3D {1=3D>2,"ABC" =3D> {"a"=3D>"A","b"=3D>"B"}}.to_yaml
    > x =3D x[5...x.length]


    Great ;)

    =2D-=20
    I=C3=B1aki Baz Castillo
     
    Iñaki Baz Castillo, Jun 29, 2008
    #3
  4. Re: How to doing a "grep -v" to hide "---" line added byHash#to_yaml ?

    On Jun 29, 2008, at 11:39 AM, Shashank Agarwal wrote:

    > Try this
    >
    > x = {1=>2,"ABC" => {"a"=>"A","b"=>"B"}}.to_yaml
    > x = x[5...x.length]


    Which can be, simply:

    {1=>2,"ABC" => {"a"=>"A","b"=>"B"}}.to_yaml[5..-1]

    It may even be slightly more efficient to use String#sub

    {1=>2,"ABC" => {"a"=>"A","b"=>"B"}}.to_yaml.sub("--- \n", '')
     
    Stephen Celis, Jun 29, 2008
    #4
  5. Re: How to doing a "grep -v" to hide "---" line added byHash#to_yaml ?

    Stephen Celis wrote:
    > Which can be, simply:
    >
    > {1=>2,"ABC" => {"a"=>"A","b"=>"B"}}.to_yaml[5..-1]
    >
    > It may even be slightly more efficient to use String#sub
    >
    > {1=>2,"ABC" => {"a"=>"A","b"=>"B"}}.to_yaml.sub("--- \n", '')


    Awesome. Totally forgot about negative indexes.
    --
    Posted via http://www.ruby-forum.com/.
     
    Shashank Agarwal, Jun 29, 2008
    #5
  6. Iñaki Baz Castillo

    phlip Guest

    Re: How to doing a "grep -v" to hide "---" line added by Hash#to_yaml?

    Shashank Agarwal wrote:

    >> {1=>2,"ABC" => {"a"=>"A","b"=>"B"}}.to_yaml.sub("--- \n", '')

    >
    > Awesome. Totally forgot about negative indexes.


    I would use the .sub option because it is explicit about what it is removing.

    However, the Hash did not add the --- . It is part of the YAML standard as the
    start of a document. You should consider leaving it alone, because other YAML
    tools will like it.

    --
    Phlip
     
    phlip, Jun 29, 2008
    #6
  7. Re: How to doing a "grep -v" to hide "---" line added byHash#to_yaml ?

    El Domingo, 29 de Junio de 2008, phlip escribi=C3=B3:
    > Shashank Agarwal wrote:
    > >> {1=3D>2,"ABC" =3D> {"a"=3D>"A","b"=3D>"B"}}.to_yaml.sub("--- \n", '')

    > >
    > > Awesome. Totally forgot about negative indexes.

    >
    > I would use the .sub option because it is explicit about what it is
    > removing.
    >
    > However, the Hash did not add the --- . It is part of the YAML standard as
    > the start of a document. You should consider leaving it alone, because
    > other YAML tools will like it.


    Yes, that's true, that "---" is part of the YAML grammar, but the fact is t=
    hat=20
    I'm only showing it, printing it, no more.

    Thanks.

    =2D-=20
    I=C3=B1aki Baz Castillo
     
    Iñaki Baz Castillo, Jun 29, 2008
    #7
  8. Iñaki Baz Castillo

    phlip Guest

    Re: How to doing a "grep -v" to hide "---" line added by Hash#to_yaml?

    >> However, the Hash did not add the --- . It is part of the YAML standard as
    >> the start of a document. You should consider leaving it alone, because
    >> other YAML tools will like it.

    >
    > Yes, that's true, that "---" is part of the YAML grammar, but the fact is that
    > I'm only showing it, printing it, no more.


    I too have automatically whacked it, for that reason. (-:
     
    phlip, Jun 30, 2008
    #8
    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. rp
    Replies:
    1
    Views:
    582
    red floyd
    Nov 10, 2011
  2. Ara.T.Howard

    to_yaml and options hash

    Ara.T.Howard, Feb 27, 2004, in forum: Ruby
    Replies:
    0
    Views:
    226
    Ara.T.Howard
    Feb 27, 2004
  3. Shin guey Wong

    Ruby Hash/Array to_yaml is slow

    Shin guey Wong, May 3, 2008, in forum: Ruby
    Replies:
    1
    Views:
    164
    Ryan Davis
    May 3, 2008
  4. Greg Hauptmann
    Replies:
    7
    Views:
    158
    Greg Hauptmann
    Sep 5, 2009
  5. Farrel Lifson
    Replies:
    1
    Views:
    189
    Farrel Lifson
    Nov 17, 2009
Loading...

Share This Page