You could do that as follows:
a2.concat(a1.delete_if { |e| a2.include?(e) })
Here's an example:
class MyClass
attr_reader :a, :b, :c
def initialize(a, b, c)
@a, @b, @c = a, b, c
end
def ==(other)
self.a == other.a && self.b == other.b
end
end
a1 = [MyClass.new('cat', 'bat', 'rat'),
MyClass.new('dog', 'hog', 'pig'),
MyClass.new('jay', 'bee', 'fly'),]
#=> [#<MyClass:0x007fca8407b678 @a="cat", @b="bat", @c="rat">,
# #<MyClass:0x007fca8407bee8 @a="dog", @b="hog", @c="pig">,
# #<MyClass:0x007fca84073ef0 @a="jay", @b="bee", @c="fly">]
a2 = [MyClass.new('fly', 'bee', 'bat'),
MyClass.new('cat', 'bat', 'rat'),
MyClass.new('dog', 'hog', 'cat'),]
#=> [#<MyClass:0x007fca840382d8 @a="fly", @b="bee", @c="bat">,
# #<MyClass:0x007fca840381e8 @a="cat", @b="bat", @c="rat">,
# #<MyClass:0x007fca840380d0 @a="dog", @b="hog", @c="cat">]
a2.concat(a1.delete_if { |e| a2.include?(e) })
#=> [#<MyClass:0x007f96d404ea08 @a="fly", @b="bee", @c="bat">,
# #<MyClass:0x007f96d404e8c8 @a="cat", @b="bat", @c="rat">,
# #<MyClass:0x007f96d404e710 @a="dog", @b="hog", @c="cat">,
# #<MyClass:0x007f96d409d9c8 @a="jay", @b="bee", @c="fly">]
a1
#=> [#<MyClass:0x007f96d409d9c8 @a="jay", @b="bee", @c="fly">]
If we change the first element of a1 from:
MyClass.new('cat', 'bat', 'rat')
to:
MyClass.new('cat', 'rat', 'bat')
we obtain:
a2.concat(a1.delete_if { |e| a2.include?(e) })
#=> [#<MyClass:0x007f89528568c0 @a="fly", @b="bee", @c="bat">,
# #<MyClass:0x007f8952856708 @a="cat", @b="bat", @c="rat">,
# #<MyClass:0x007f89528562d0 @a="dog", @b="hog", @c="cat">,
# #<MyClass:0x007f89519277f0 @a="cat", @b="rat", @c="bat">,
# #<MyClass:0x007f8951927598 @a="jay", @b="bee", @c="fly">]