[ruby-core:96078] [Ruby master Bug#16395] .any? and .all? methods, flawed.

[email protected]
Newsgroups gmane.comp.lang.ruby.core
Message-ID <redmine.journal-82928.20191203122123.08781c1d7ca663ef@ruby-lang.org>
Issue #16395 has been updated by mame (Yusuke Endoh).

Status changed from Open to Rejected

@Hanmac 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.

----------------------------------------
Bug #16395: .any? and .all? methods, flawed.
https://bugs.ruby-lang.org/issues/16395#change-82928

* Author: stiuna (Juan Gregorio)
* Status: Rejected
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 2.6.5
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
The dynamics of the script is simple, no explanations needed, look for yourselves:

``` ruby
#                 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:

```
100E1
312A3
8B64
282F
C4A
B28F
Coincidences: 6
```

That's correct. Now change .all? for .any?

Out:

```
100E1
Coincidences: 1
```

That's not right. Now let's modify the .any? block like this:

``` ruby
virus.any? {|i|
    if result.include? i
        count += 1
        puts "#{count}:#{i}"
    end
}
```

Out:

```
1:100E1
2:312A3
3:8B64
4:282F
5:C4A
6:B28F
Coincidences: 6
```

That's correct. Now change .any? for .all?

Out:

```
1:100E1
Coincidences: 1
```

That's not right. Now let's modify the .all? block like this:

``` ruby
virus.all? {|i|
    if result.include? i
        puts "#{count}:#{i}"
        count += 1
    end
}
```

Out:

```
0:100E1
1:312A3
2:8B64
3:282F
4:C4A
5:B28F
Coincidences: 6
```

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:

``` ruby
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?

``` ruby
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.**




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.