If I want to compare two arrays and create an interpolated output string if an array variable from array y
exists in x
how can I get an output for each matching element?
This is what I was trying but not quite getting the result.
x = [1, 2, 4] y = [5, 2, 4] x.each do |num| puts " The number #{num} is in the array" if x.include?(y.each) end #=> [1, 2, 4]
You can use the set intersection method &
for that:
x = [1, 2, 4] y = [5, 2, 4] x & y # => [2, 4]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With