Ruby:将转义的字符串写入YAML [英] Ruby: Write escaped string to YAML

查看:110
本文介绍了Ruby:将转义的字符串写入YAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下...

  require'yaml'
test =我是一个b& string
File.open('test.yaml','w')do | out |
out.write(test.to_yaml)
end

...输出...

  ---这是一个b& d字符串
pre>

如何让它输出

  --- '这是一个b& d字符串'

???

解决方案

如果要在YAML中存储转义的字符串,
将使用 #inspect 转义它您将其转换为YAML:

  irb>需要'yaml'
=> true
irb> str =%{这个字符串有点复杂,但它做的工作(男人,我讨厌吓人的报价)}
=> 这个字符串有点复杂,但是做这个工作(人,我讨厌吓人的报价)
irb> str
这个字符串有点复杂,但它做的工作(人,我讨厌吓人的报价)
=> nil
irb>放str.inspect
这个字符串有点复杂,但它\做的工作(人,我讨厌吓人的报价)
=> nil
irb>放str.to_yaml
---这个字符串有点复杂,但它做的工作(人,我讨厌吓人的报价)
=> nil
irb>放str.inspect.to_yaml
---\这个字符串有点复杂,但它\\\做的工作\\\\(人,我讨厌吓人的报价) \
=> nil

YAML不引用字符串,除非它必须。它引用字符串,如果它们包含它会丢失的东西,如果它存储它不引用 - 像周围的引号字符或尾随或前导空格:

  IRB> puts(str +).to_yaml 
---这个字符串有点复杂,但它是\\做的工作\\(男人,我讨厌吓人的报价)
=> nil
irb> put%{#{str}} to_yaml
---\这个字符串有点复杂,但是\\做的工作(人,我讨厌吓人的报价)\
=> nil
irb> put(+ str).to_yaml
---这个字符串有点复杂,但是\做的工作(我讨厌吓人的报价)
=> nil

但是,作为YAML消费者,字符串引用对您来说无关紧要。你永远不应该自己解析YAML文本 - 把它留给图书馆。如果你需要在YAML文件中引用的字符串,那对我来说很糟糕。



你的字符串是否有他们中,YAML将保留字符串:

  irb> test =我是一个b& d字符串
=> 我是一个b& d字符串
irb> YAML :: load(YAML :: dump(test))
=> 我是一个b& d字符串
irb> YAML :: load(YAML :: dump(test))== test
=>真


The following...

require 'yaml'
test = "I'm a b&d string"
File.open('test.yaml', 'w') do |out|
  out.write(test.to_yaml)
end

...outputs ...

--- this is a b&d string

How can I get it to output

--- 'this is a b&d string'

???

解决方案

If you want to store an escaped string in YAML, escape it using #inspect before you convert it to YAML:

irb> require 'yaml'
=> true
irb> str = %{This string's a little complicated, but it "does the job" (man, I hate scare quotes)}
=> "This string's a little complicated, but it \"does the job\" (man, I hate scare quotes)"
irb> puts str
This string's a little complicated, but it "does the job" (man, I hate scare quotes)
=> nil
irb> puts str.inspect
"This string's a little complicated, but it \"does the job\" (man, I hate scare quotes)"
=> nil
irb> puts str.to_yaml
--- This string's a little complicated, but it "does the job" (man, I hate scare quotes)
=> nil
irb> puts str.inspect.to_yaml
--- "\"This string's a little complicated, but it \\\"does the job\\\" (man, I hate scare quotes)\""
=> nil

YAML doesn't quote strings unless it has to. It quotes strings if they include things that it would miss if it stored it unquoted - like surrounding quote characters or trailing or leading spaces:

irb> puts (str + " ").to_yaml
--- "This string's a little complicated, but it \"does the job\" (man, I hate scare quotes) "
=> nil
irb> puts %{"#{str}"}.to_yaml
--- "\"This string's a little complicated, but it \"does the job\" (man, I hate scare quotes)\""
=> nil
irb> puts (" " + str).to_yaml
--- " This string's a little complicated, but it \"does the job\" (man, I hate scare quotes)"
=> nil

However, as a YAML consumer, whether the string is quoted shouldn't matter to you. You should never be parsing the YAML text yourself - leave that to the libraries. If you need the string to be quoted in the YAML file, that smells bad to me.

It doesn't matter whether your strings have '&'s in them, YAML will preserve the string:

irb> test = "I'm a b&d string"
=> "I'm a b&d string"
irb> YAML::load(YAML::dump(test))
=> "I'm a b&d string"
irb> YAML::load(YAML::dump(test)) == test
=> true

这篇关于Ruby:将转义的字符串写入YAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆