%w[ ] Non-interpolated Array of words, separated by whitespace
%W[ ] Interpolated Array of words, separated by whitespace
用法:Ruby中的%w {}和%W {}大小写W数组字面量有什么区别?
p %w{one one two three 0 1 1 2 3} # = > ["one", "one", "two", "three", "0", "1", "1", "2", "3"]
p %W{one one two three 0 1 1 2 3} # = > ["one", "one", "two", "three", "0", "1", "1", "2", "3"]
p %w{C:\ C:\Windows} # => ["C: C:\\Windows"]
p %W{C:\ C:\Windows} # => ["C: C:Windows"]
我的问题是...有什么区别?
检查编辑,我喜欢你的例子,它解释了很多。在我的例子中最后两行的输出让我困惑。 – RyanScottLewis 2011-04-22 03:01:10
同样,你没有使用Ruby表达式,它没有被“#{}”包围。反斜杠是Ruby中的一个特殊字符,用于从字面上解释其他特殊字符,并且您在字符串的上下文中使用该字符,而不是表达式。这就解释了为什么反斜杠在双引号中反弹而不是单引号。 – acconrad 2011-04-22 03:04:13
我认为c00lryguy的观点更深入。 1)尽管'\''='“\\”',反斜杠与'%w {}'一起消失了。 2)在''W \''''''W''中,如'%W {}'所示,反斜杠被忽略的时间也是一个普遍的问题。 – sawa 2011-04-22 03:16:19