简体   繁体   English

红宝石to_yaml行为

[英]Strange ruby to_yaml behavior

I got this weird problem with difference in behavior of to_yaml . 我有一个奇怪的问题,与to_yaml行为不同。 For: 对于:

"0111".to_yaml

it returns: 它返回:

"--- \"0111\"\n"

At the same time this: 同时这:

"0128".to_yaml

returns: 收益:

"--- 0128\n"

As you can see the first output have "" but the second doesn't. 如您所见,第一个输出具有""而第二个则没有。

Any idea? 任何想法?

Environment: MRI Ruby 1.9.2 and JRuby 1.6.5 (1.9 mode). 环境:MRI Ruby 1.9.2和JRuby 1.6.5(1.9模式)。

I believe Eugene is right about the reason for these issue - seems like parser treats strings containing valid octal numbers differently. 我相信Eugene对于这些问题的原因是正确的-似乎解析器对待包含有效八进制数字的字符串的处理方式有所不同。

If you don't like it you can switch to a different YAML parser. 如果您不喜欢它,可以切换到其他YAML解析器。 According to docs there are two parsers available: Syck and Psych . 根据文档 ,有两个解析器可用: SyckPsych The former is old and unmaintained and the latter is its replacement. 前者是旧的,没有维护,而后者是它的替代品。

Psych is used in latest versions of Rails and is default YAML engine in 1.9.3. Psych用于最新版本的Rails中,并且是1.9.3中的默认YAML引擎。 But you can easily use it in 1.9.2 and see the difference: 但是您可以轻松地在1.9.2中使用它并看到不同之处:

require 'yaml'

p YAML::ENGINE.yamler      # => "syck"
# or explicitly set YAML::ENGINE.yamler = "syck" in 1.9.3
p '01'.to_yaml             # => "--- \"01\"\n"
p '08'.to_yaml             # => "--- 08\n"
p YAML.load('01'.to_yaml)  # => "01"
p YAML.load('08'.to_yaml)  # => "08"

p YAML::ENGINE.yamler = "psych"
p '01'.to_yaml             # => "--- '01'\n"
p '08'.to_yaml             # => "--- '08'\n"
p YAML.load('01'.to_yaml)  # => "01"
p YAML.load('08'.to_yaml)  # => "08"

As you can see there is no need to worry about different representation of strings in Syck as long as you're using the same engine to decode data (in both cases it returns original strings). 如您Syck ,只要您使用相同的引擎解码数据(在两种情况下均返回原始字符串),就无需担心Syck中字符串的不同表示形式。

If for some reason you need unified representation for strings in YAML you can switch to Psych (it's more consistent at least in this case), but be careful as you might get in troubles trying to load with Psych data that was previously dumped with Syck . 如果出于某种原因需要在YAML中统一表示字符串,则可以切换到Psych (至少在这种情况下更一致),但是要小心,因为可能难以加载以前由Syck转储的Psych数据。

I can duplicate this as well. 我也可以复制。 I'm not sure what is doing it, but my first guess is that it has do with the "numbers" in the string actually being octal numbers. 我不确定该怎么做,但是我的第一个猜测是它与字符串中的“数字”实际上是八进制数字有关。 Or more accurately, the first being octal, and the second not. 或更准确地说,第一个是八进制,第二个不是八进制。 If you remove the 0, you get the behavior from the first on both of them. 如果删除0,则会从第一个开始获得行为。 Perhaps somebody else could elaborate on this theory. 也许其他人可以阐述这个理论。

1.9.2p290 :002 > "0111".to_yaml
  => "--- \"0111\"\n" 
1.9.2p290 :003 > "0128".to_yaml
  => "--- 0128\n" 
1.9.2p290 :004 > "\0111".to_yaml
  => "--- \"\\t1\"\n" 
1.9.2p290 :005 > "\0128".to_yaml
  => "--- \"\\n\\\n8\"\n" 
1.9.2p290 :006 > string = "0111"
  => "0111" 
1.9.2p290 :007 > string.class
 => String 
1.9.2p290 :008 > string.to_yaml
 => "--- \"0111\"\n" 
1.9.2p290 :009 > string = "111"
 => "111" 
1.9.2p290 :010 > string.to_yaml
 => "--- \"111\"\n" 
1.9.2p290 :011 > string = "128"
 => "128" 
1.9.2p290 :012 > string.to_yaml
 => "--- \"128\"\n"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM