Vectorized string concatenation in Ruby, e.g. R paste function

I have two arrays in Ruby that I would like to combine together. In R, it is as simple as using the paste function, because it is vectorized:

 # 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" 

In Ruby, this is a bit more complicated, and I would like to know if there is a more direct way:

 # 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"] 
+4
source share
1 answer

Alternative way:

 values = [1, 2, 3] names = ["one", "two", "three"].to_enum values.map{|v|"#{v} as #{names.next}"} # => ["1 as one", "2 as two", "3 as three"] 

This, however, is complicated with more than two arrays. The OP version works better with multiple arrays.

+3
source

All Articles