我在 Ruby 中有两个数组,我想将它们按元素连接在一起。在 R 中,这就像使用paste
函数一样简单,因为它是矢量化的:
# R
values <- c(1, 2, 3)
names <- c("one", "two", "three")
paste(values, names, sep = " as ")
[1] "1 as one" "2 as two" "3 as three"
在Ruby中它有点复杂,我想知道是否有更直接的方法:
# Ruby
values = [1, 2, 3]
names = ["one", "two", "three"]
values.zip(names).map { |zipped| zipped.join(" as ") }
=> ["1 as one", "2 as two", "3 as three"]