带有空格的单词数组

时间:2012-12-10 21:24:37

标签: ruby 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'"] 

如何将空白作为元素?

1 个答案:

答案 0 :(得分:15)

您可以转义空格字符

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

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