ruby array minus array
To subtract one array from another in Ruby, you can use the "-" operator. This will remove any elements from the first array that are also present in the second array. Here is an example:
array1 = [1, 2, 3, 4, 5]
array2 = [3, 4, 5, 6, 7]
result = array1 - array2
Explanation of each step:
- We declare an array called
array1
and assign it the values [1, 2, 3, 4, 5]. - We declare another array called
array2
and assign it the values [3, 4, 5, 6, 7]. - We use the "-" operator to subtract
array2
fromarray1
, resulting in a new array calledresult
. - The elements [3, 4, 5] are present in both
array1
andarray2
, so they are removed fromarray1
in theresult
. - The final value of
result
is [1, 2].
This is how you can subtract one array from another in Ruby using the "-" operator.