I am a little confused with Enumerator#reject in ruby. Consider the following code:
(1..10).select {|i| i % 3 == 0 || i % 5 == 0 } => [3, 5, 6, 9, 10]
Shouldn’t the following line be equivalent?
(1..10).reject {|i| i % 3 != 0 || i % 5 != 0 } => []
If I just use one condition on the reject method, the result is as expected. but If I include the OR operator the result turns out to be empty. Could somebody clarify this for me.
(1..10).reject {|i| i % 3 != 0} => [3, 6, 9]
You are making a basic logic mistake:
!(A || B)is equivalent to!A && !Band NOT equivalent to!A || !B.So if you change the
||in your second example to a&&, then your second example would give the same result as the first: