Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在 Ruby 中有一个数组,比如["dog", "cat", bat"]. 如何将它们加入一个字符串,其中单词用空格分隔,例如"dog cat bat"?我知道迭代元素并一个接一个地加入它们很容易,但是有没有单行的方式?
["dog", "cat", bat"]
"dog cat bat"
a = ["dog", "cat", "bat"] a.join(" ") #=> "dog cat bat"
可爱的捷径:
["dog", "cat", "bat"] * ' ' #=> "dog cat bat"
[“狗”、“猫”、“蝙蝠”].join ' '