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

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

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
 
S

Shashank Agarwal

Iñaki Baz Castillo said:
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]
 
I

Iñaki Baz Castillo

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
 
S

Stephen Celis

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", '')
 
S

Shashank Agarwal

Stephen said:
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.
 
P

phlip

Shashank said:
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.
 
I

Iñaki Baz Castillo

El Domingo, 29 de Junio de 2008, phlip escribi=C3=B3:
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
 
P

phlip

However, the Hash did not add the --- . It is part of the YAML standard as
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. (-:
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top