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.
我的数组如下所示:
arr = ["object 1", 3, "object 2", 6]
如何在下面创建预期的输出?有没有简单的使用方法join?
join
object 1 (x3), object 2 (x6).
从你的数组到你的字符串没有逻辑转换,所以我认为你应该重新考虑一下。 但你可以做
arr.each_slice(2). # get every two elements map { |top| "#{top.first} (x#{top.last})" }. # create a string for each pair join(', ') # join them by a comma. # => "object 1 (x3), object 2 (x6)"