Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to doing a "grep -v" to hide "---" line added by Hash#to_yaml ?

Reply
Thread Tools

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

 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      06-29-2008
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

 
Reply With Quote
 
 
 
 
Shashank Agarwal
Guest
Posts: n/a
 
      06-29-2008
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/.

 
Reply With Quote
 
 
 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      06-29-2008
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

 
Reply With Quote
 
Stephen Celis
Guest
Posts: n/a
 
      06-29-2008
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", '')

 
Reply With Quote
 
Shashank Agarwal
Guest
Posts: n/a
 
      06-29-2008
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/.

 
Reply With Quote
 
phlip
Guest
Posts: n/a
 
      06-29-2008
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
 
Reply With Quote
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      06-29-2008
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

 
Reply With Quote
 
phlip
Guest
Posts: n/a
 
      06-30-2008
>> 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. (-:
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
asp:checkbox hide/show text/fields when it's clicked without doing a postback? UJ ASP .Net 4 04-24-2009 04:50 PM
Solution for posterity: GridView, Datakeys, and "Item has already been added. Key in dictionary: 'CategoryID' Key being added: 'CategoryID'" ASP .Net 2 11-02-2006 04:48 AM
[HIDE LAN] Hide a part of hosts into the LAN at VPN users? Paolo Bresi Cisco 1 04-04-2005 02:41 PM
how come user control added to page doesn't get added to codebehind file? Bennett Haselton ASP .Net 1 11-08-2004 09:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57