可能是以下方法的名称:
像这样:
[1,2,3,4,5].some_method(['a', 'b', 'c'])
#=> ['a', 'b', 'c', 4, 5]
我在标准库中找不到任何东西。 "阵列合并"在其他问题中,有时会引用Array#zip
,Array#concat
或Array#transpose
,但这些不是我正在寻找的。 p>
最接近的是Hash#merge
,没有块,索引为键:
{1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5}.merge({1 => 'a', 2 => 'b', 3 => 'c'}).values
#=> ['a', 'b', 'c', 4, 5]
比merge
更好的名字是什么? merge_by_index
,overlay
,override
,......?在其他语言中是否有已知的等效方法?
是否有比这些更短或更清洁的实施?
class Array
# 1st possibility
def merge(other_array)
n = [size, other_array.size].max
(0...n).map { |i| other_array[i] || self[i] } # Doesn't work when overriding with nil/false
end
# 2nd possibility
def merge(other_array)
d = size - other_array.size
if d > 0
other_array + self[-d, d]
else
other_array
end
end
# 3rd possibility
def merge(other_array)
hash = map.with_index { |x, i| [i, x] }.to_h
other_hash = other_array.map.with_index { |x, i| [i, x] }.to_h
hash.merge(other_hash).values
end
end
以下是一些基本测试:
default = [1, 2, 3, 4, 5]
array = %w(a b c)
p default.merge(array) == ['a', 'b', 'c', 4, 5]
p default.merge([false, nil, true]) == [false, nil, true, 4, 5]
p default.merge((0..9).to_a) == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
p default.merge((2..6).to_a) == [2, 3, 4, 5, 6]
p [].merge((2..6).to_a) == [2, 3, 4, 5, 6]
p [1, 2, 3].merge([]) == [1, 2, 3]
答案 0 :(得分:3)
<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081"/>
<flow name="test_flow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="NEW HTTP Connector"/>
<...>
</flow>