Rubyに要素の配列があります
[2,4,6,3,8]
3
たとえば、値を持つ要素を削除する必要があります
それ、どうやったら出来るの?
私はそれを理解したと思います:
a = [3, 2, 4, 6, 3, 8]
a.delete(3)
#=> 3
a
#=> [2, 4, 6, 8]
コメントでTravisから借りると、これはより良い答えです。
私は個人的に
[1, 2, 7, 4, 5] - [7]
どちらの結果が好き=> [1, 2, 4, 5]
ですかirb
3 が彼の例の配列の 3 番目の要素であることを確認して、私は彼の答えを修正しました。これは、3 が配列の 2 番目の位置にあることに気付いていない人にとっては混乱を招く可能性があります。
別のオプション:
a = [2,4,6,3,8]
a -= [3]
その結果、
=> [2, 4, 6, 8]
誰かがこれを述べているかどうかはわかりませんが、Array.delete() と -= valueは、Array 内で渡された値のすべてのインスタンスを削除します。特定の要素の最初のインスタンスを削除するには、次のようなことができます
arr = [1,3,2,44,5]
arr.delete_at(arr.index(44))
#=> [1,3,2,5]
もっと簡単な方法があるかもしれません。これがベストプラクティスだと言っているわけではありませんが、認識されるべきものです。
-=[4]
他の回答で言及されている、値が 4 の要素を削除する方法が気に入っています。
しかし、次の方法があります。
[2,4,6,3,8,6].delete_if { |i| i == 6 }
=> [2, 4, 3, 8]
関数について言及した後、 「 Basic Array Operations 」のどこかで言及されていmap
ます。
A.delete_at(3)
3
ここがその位置です。
いくつかのベンチマークを次に示します。
require 'fruity'
class Array
def rodrigo_except(*values)
self - values
end
def niels_except value
value = value.kind_of?(Array) ? value : [value]
self - value
end
end
ARY = [2,4,6,3,8]
compare do
soziev { a = ARY.dup; a.delete(3); a }
steve { a = ARY.dup; a -= [3]; a }
barlop { a = ARY.dup; a.delete_if{ |i| i == 3 }; a }
rodrigo { a = ARY.dup; a.rodrigo_except(3); }
niels { a = ARY.dup; a.niels_except(3); }
end
# >> Running each test 4096 times. Test will take about 2 seconds.
# >> soziev is similar to barlop
# >> barlop is faster than steve by 2x ± 1.0
# >> steve is faster than rodrigo by 4x ± 1.0
# >> rodrigo is similar to niels
また、多くの重複を含むより大きな配列を使用します。
class Array
def rodrigo_except(*values)
self - values
end
def niels_except value
value = value.kind_of?(Array) ? value : [value]
self - value
end
end
ARY = [2,4,6,3,8] * 1000
compare do
soziev { a = ARY.dup; a.delete(3); a }
steve { a = ARY.dup; a -= [3]; a }
barlop { a = ARY.dup; a.delete_if{ |i| i == 3 }; a }
rodrigo { a = ARY.dup; a.rodrigo_except(3); }
niels { a = ARY.dup; a.niels_except(3); }
end
# >> Running each test 16 times. Test will take about 1 second.
# >> steve is faster than soziev by 30.000000000000004% ± 10.0%
# >> soziev is faster than barlop by 50.0% ± 10.0%
# >> barlop is faster than rodrigo by 3x ± 0.1
# >> rodrigo is similar to niels
そして、より多くの重複でさらに大きくなります:
class Array
def rodrigo_except(*values)
self - values
end
def niels_except value
value = value.kind_of?(Array) ? value : [value]
self - value
end
end
ARY = [2,4,6,3,8] * 100_000
compare do
soziev { a = ARY.dup; a.delete(3); a }
steve { a = ARY.dup; a -= [3]; a }
barlop { a = ARY.dup; a.delete_if{ |i| i == 3 }; a }
rodrigo { a = ARY.dup; a.rodrigo_except(3); }
niels { a = ARY.dup; a.niels_except(3); }
end
# >> Running each test once. Test will take about 6 seconds.
# >> steve is similar to soziev
# >> soziev is faster than barlop by 2x ± 0.1
# >> barlop is faster than niels by 3x ± 1.0
# >> niels is similar to rodrigo
したがって、3 が複数回出現し、最初に出現した 3 のみを削除したい場合は、以下のように簡単に実行できます。
arr = [2, 4, 6, 3, 8, 10, 3, 12]
arr.delete_at arr.index 3
#This will modify arr as [2, 4, 6, 8, 10, 3, 12] where first occurrence of 3 is deleted. Returns the element deleted. In this case => 3.
モンキー パッチを適用することもできます。なぜ Ruby に のexcept
メソッドがあるのに のメソッドHash
がないのか理解できませんでしたArray
:
class Array
def except value
value = value.kind_of(Array) ? value : [value]
self - value
end
end
これで、次のことができます。
[1,3,7,"436",354,nil].except(354) #=> [1,3,7,"436",nil]
または:
[1,3,7,"436",354,nil].except([354, 1]) #=> [3,7,"436",nil]