如何使用':'分解 %w'dog:cat:bird' 中的单词不带分割
我正在尝试执行 %w'dog:cat:bird'
但我希望分隔单词的字符是 : 而不是像 %w
目前那样的空格。
我不想使用 .split
因为在实际代码中我使用了一些不同的 %
习惯用法来满足不同的需求,并且我只想使用一种语法。
I am trying to do %w'dog:cat:bird'
but I want the character that breaks apart the words to be a : rather than whitespace as %w
currently does.
I do not want to use .split
as in the actual code I am using a few different %
idioms for different needs and I would like to use just one syntax.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚查阅了 Matz 和 David Flanagan 所著的《The Ruby Programming Language》,看来用 %w 创建的数组文字必须使用空格来分隔元素。如果您确实想要拥有由“:”分隔的字符串数组,并且不想在代码中使用“split”,我建议您定义一个自己的方法,该方法将允许您模拟所需的行为,也许是这样的:
然后你可以写这样的东西:
I just checked in "The Ruby Programming Language" by Matz and David Flanagan, and it appears that array literals created with %w must use spaces to delimit the elements. If you really want to have arrays of strings, delimited by ":", and you don't want to use "split" in the code, I suggest you define a method of your own which will allow you to simulate the desired behavior, maybe something like:
Then you can write something like: