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

**URL:** <https://www.ruby-forum.com/t/how-to-doing-a-grep-v-to-hide-line-added-by-hash-to-yaml/141697>\
**Category:** Ruby\
**Created:** [June 29, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/how-to-doing-a-grep-v-to-hide-line-added-by-hash-to-yaml/141697 "2008-06-29T17:37:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 29, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/how-to-doing-a-grep-v-to-hide-line-added-by-hash-to-yaml/141697/1 "2008-06-29T17:37:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 29, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/how-to-doing-a-grep-v-to-hide-line-added-by-hash-to-yaml/141697/2 "2008-06-29T18:42:25Z")

</div>

IÃ±aki Baz C. 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]

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 29, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/how-to-doing-a-grep-v-to-hide-line-added-by-hash-to-yaml/141697/3 "2008-06-29T18:53:16Z")

</div>

El Domingo, 29 de Junio de 2008, Shashank A. escribiÃ³:

> Try this
> 
> x = {1=\>2,“ABC” =\> {“a”=\>“A”,“b”=\>“B”}}.to\_yaml  
> x = x[5…x.length]

Great 😉

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 29, 2008, 6:58pm UTC](https://www.ruby-forum.com/t/how-to-doing-a-grep-v-to-hide-line-added-by-hash-to-yaml/141697/4 "2008-06-29T18:58:34Z")

</div>

On Jun 29, 2008, at 11:39 AM, Shashank A. 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", ‘’)

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 29, 2008, 11:36pm UTC](https://www.ruby-forum.com/t/how-to-doing-a-grep-v-to-hide-line-added-by-hash-to-yaml/141697/5 "2008-06-29T23:36:08Z")

</div>

Shashank A. 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.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 29, 2008, 11:59pm UTC](https://www.ruby-forum.com/t/how-to-doing-a-grep-v-to-hide-line-added-by-hash-to-yaml/141697/6 "2008-06-29T23:59:32Z")

</div>

El Domingo, 29 de Junio de 2008, phlip escribiÃ³:

> 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.

Thanks.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 29, 2008, 7:03pm UTC](https://www.ruby-forum.com/t/how-to-doing-a-grep-v-to-hide-line-added-by-hash-to-yaml/141697/7 "2008-06-29T19:03:00Z")

</div>

Stephen C. 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.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 30, 2008, 2:23pm UTC](https://www.ruby-forum.com/t/how-to-doing-a-grep-v-to-hide-line-added-by-hash-to-yaml/141697/8 "2008-06-30T14:23:40Z")

</div>

> > 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. (-:
