8

我开始喜欢word arrays,但今天我面临一个挑战:

 > a = %w[ faq contact 'about us' legal 'bug reports' ]
 => ["faq", "contact", "'about", "us'", "legal", "'bug", "reports'"] 
 > a = %w[ faq contact "about us" legal 'bug reports' ]
 => ["faq", "contact", "\"about", "us\"", "legal", "'bug", "reports'"] 

我怎样才能有空白元素?

4

1 回答 1

16

您可以转义空格字符

a = %w[ faq contact about\ us legal bug\ reports ]
a # => ["faq", "contact", "about us", "legal", "bug reports"]

但我仍然会考虑使用“完整”数组文字。在这种情况下,它们不那么令人困惑。

于 2012-12-10T21:26:23.860 回答