Emit YAML without leading hyphens?

B

bwv549

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!
 
R

Ryan Davis

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})


As I understand it, if you remove the hyphens, it is no longer yaml,
so don't do that.
 
J

Joel VanderWerf

Ryan said:
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})


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]
 
A

Alex Young

Henrik said:
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.
 
B

bwv549

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

Marc Heiler

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

Simon Chiang

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/options-for-rubys-@to_yaml@-method

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.
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top