ruby – 带有空格的Word数组

前端之家收集整理的这篇文章主要介绍了ruby – 带有空格的Word数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我开始喜欢单词数组,但今天我面临挑战:
> 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","\"about","us\"","reports'"]

我怎么能有空格元素?

解决方法

您可以转义空格字符
a = %w[ faq contact about\ us legal bug\ reports ]
a # => ["faq","about us","bug reports"]

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

原文链接:https://www.f2er.com/ruby/269315.html

猜你在找的Ruby相关文章