例如
a = [a,b,c]
b = [1,2,3]
我想:
[a1,b2,c3]
a, b 长度相同,都是字符串数组。
有什么本地方法可以做到这一点吗?
%w(a b c).zip(%w(1 2 3))
# => [["a","1"],["b","2"],["c","3]]
如果要连接字符串:
%w(a b c).zip(%w(1 2 3)).map(&:join)
# => ["a1","b2","c3"]
这扩展到更多数组:
%w(a b c).zip(%w(1 2 3), %w(x y z)).map(&:join)
# => ["a1x", "b2y", "c3z"]
例如,您可以这样做
a = %w{a b c}
b = %w{1 2 3}
c = a.map.with_index do |elem, idx|
elem + b[idx]
end
c # => ["a1", "b2", "c3"]