ruby - 带空格的单词数组

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

我怎样才能让空白成为一个元素?

最佳答案

可以转义空格

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

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

关于ruby - 带空格的单词数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13809517/

相关文章:

ruby - Ruby 排序函数到底做了什么?

ruby - Rspec : expect vs expect with block - what's the difference?

java - 如何将图像添加到图像数组?

c - 二维数组C的快速输出

c++ - 如何初始化 char*[] 数组?

php - mysql_fetch_array 并且只有字符串数组键

ruby - 使用什么机制允许从该范围调用 ruby​​ root 范围中定义的方法?

sql - 在大句子中搜索字符串时优化查询的最佳方法

ruby - 我的 ruby 在哪里?

JavaScript-仅从数组中获取负数