-1

例如

a = [a,b,c]
b = [1,2,3]

我想:

[a1,b2,c3]

a, b 长度相同,都是字符串数组。

有什么本地方法可以做到这一点吗?

4

2 回答 2

3
%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"]
于 2012-10-27T18:20:51.827 回答
1

例如,您可以这样做

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"]
于 2012-10-27T18:19:11.043 回答