Bug #16395
closed.any? and .all? methods, flawed.
Description
The dynamics of the script is simple, no explanations needed, look for yourselves:
# 100E1 312A3 8B64 282F C4A B28F
result = '99E2E403100E1A685A63312A308B6478282F2B2C4A34B28F7E'
virus = ['100E1', '312A3', '8B64', '282F', 'C4A', 'B28F']
count = 0
virus.all? {|i|
puts i
if result.include? i
count += 1
end
}
puts "Coincidences: #{count}"
Out:
That's correct. Now change .all? for .any?
Out:
That's not right. Now let's modify the .any? block like this:
Out:
That's correct. Now change .any? for .all?
Out:
That's not right. Now let's modify the .all? block like this:
Out:
That's correct. If you look only I have changed the order of the instructions puts and count.
In case you don't understand me, I'll put the complete code with each method:
result = '99E2E403100E1A685A63312A308B6478282F2B2C4A34B28F7E'
virus = ['100E1', '312A3', '8B64', '282F', 'C4A', 'B28F']
count = 0
virus.any? {|i|
if result.include? i
# If these two lines change order the program fails.
count += 1
puts "#{count}:#{i}"
end
}
puts "Coincidences: #{count}"
The output will print 6 coincidences, but if you change the order of the instructions puts and count the program returns 1 coincidence.
Now with .all?
result = '99E2E403100E1A685A63312A308B6478282F2B2C4A34B28F7E'
virus = ['100E1', '312A3', '8B64', '282F', 'C4A', 'B28F']
count = 0
virus.all? {|i|
if result.include? i
# If these two lines change order the program fails. Compare the order with .any?
puts "#{count}:#{i}"
count += 1
end
}
puts "Coincidences: #{count}"
The output will print 6 coincidences, but if you change the order of the instructions puts and count the program returns 1 coincidence.
Updated by Hanmac (Hans Mackowiak) almost 7 years ago
your problem is that .any? and .all? wants a value returned for the block which is truly or falsely
puts returns nil which is falsely, in the second example count += 1 returns an integer which is truly
EDIT: it looks like you might want to check out virus.count { |i| result.include? i } ?
Updated by mame (Yusuke Endoh) almost 7 years ago
- Status changed from Open to Rejected
@Hanmac (Hans Mackowiak) is right.
ary = [1, 2, 3, 4, 5]
result = ary.any? {|n| p n; n == 3 } #=> 1, 2, 3
p result #=> true
result = ary.all? {|n| p n; n != 3 } #=> 1, 2, 3
p result #=> false
You may want to use each instead of any? and all?, I guess.
Updated by shevegen (Robert A. Heiler) almost 7 years ago
The dynamics of the script is simple, no explanations needed, look for yourselves:
I do not think that this can be a useful issue description because there may be
secondary aspect to consider for when requesting changes, including backwards
compatibility after a given change/suggestion. So even if there were a problem in
regards to the behaviour, it would be better to include these considerations in the
issue tracker, even if you think that any behaviour is flawed or not optimal.
If you work with .any? .select and .reject, I personally found it better to split
up the tasks, finish working on the dataset, and then do a separate reporting. Right
now you combine both in one go, which can lead to the confusion you described.