Forum: Ruby Emit YAML without leading hyphens?

883ee4d990ef9e57a79b5b0ff3b66241?d=identicon&s=25 bwv549 (Guest)
on 2009-04-07 23:18
(Received via mailing list)
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!
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2009-04-08 00:28
(Received via mailing list)
On Apr 7, 2009, at 14:14 , bwv549 wrote:

> 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.
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-04-08 00:43
(Received via mailing list)
Ryan Davis wrote:
>>    [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]
4e39ce958e5ab660993b8b175b129803?d=identicon&s=25 Henrik Hodne (Guest)
on 2009-04-08 12:03
(Received via mailing list)
Just a quick question: Why do you want to do this?

Regards,
Henrik Hodne
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (regularfry)
on 2009-04-08 17:53
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
883ee4d990ef9e57a79b5b0ff3b66241?d=identicon&s=25 bwv549 (Guest)
on 2009-04-09 23:00
(Received via mailing list)
> 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.
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-04-10 08:39
(Received via mailing list)
On Thu, Apr 9, 2009 at 11:00 PM, bwv549 <jtprince@gmail.com> 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
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2009-04-10 12:58
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.
2e2d8c8ff86e18bf49f777f39028823e?d=identicon&s=25 Simon Chiang (bahuvrihi)
on 2009-04-10 16:42
(Received via mailing list)
> 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...

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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.