Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape an & (ampersand) at the start of a YAML entry?

Tags:

yaml

An ampersand at the start of a YAML entry is normally seen as a label for a set of data that can be referenced later. How do you escape a legitimate ampersand at the start of a YAML entry. For example:

---
- news:
    news_text: “Text!’

I am looking to not have &ldquo be a label within the yaml file, but rather when I get parse the YAML file to have the news_text come back with the “ in the entry.

like image 965
Douglas Sellers Avatar asked Feb 18 '10 18:02

Douglas Sellers


People also ask

What is called an escape?

Verb. escape, avoid, evade, elude, shun, eschew mean to get away or keep away from something. escape stresses the fact of getting away or being passed by not necessarily through effort or by conscious intent.

What is an example of escape?

[count] : an act of escaping from a place, situation, etc. The prisoners attempted a daring escape. He celebrated his escape from his boring job with a long vacation. She had a lucky escape when she wasn't injured in the accident.

Is it escape from or escape?

escape from somebody/something He escaped from prison this morning. She attempted to escape from the pirates holding her hostage. escape somebody/something She managed to escape her captors. He escaped prison with two other inmates.

How do you use escape as a verb?

[transitive, no passive] to avoid something unpleasant or dangerous escape something She was lucky to escape punishment. The pilot escaped death by seconds. There was no escaping the fact that he was overweight. escape doing something He narrowly escaped being killed.


2 Answers

Just put quotes around the text

require 'yaml'

data = <<END
---
- news:
    news_text: "&ldquo;Text!&rsquo;"
END

puts YAML::load(data).inspect

# produces => [{"news"=>{"news_text"=>"&ldquo;Text!&rsquo;"}}]
like image 93
dan Avatar answered Oct 17 '22 15:10

dan


You probably can enclose the text in quotes:

---
- news:
    news_text: "&ldquo;Text!&rsquo;"

Besides, you can probably just as well use the proper characters there:

---
- news:
    news_text: “Text!’

Putting escapes specific to a totally different markup language into a document written in another markup language seems ... odd to me, somehow.

like image 38
Joey Avatar answered Oct 17 '22 16:10

Joey