[ruby-core:96321] [Ruby master Feature#16039] Array#contains? to check if one array contains another array

[email protected]
Newsgroups gmane.comp.lang.ruby.core
Message-ID <redmine.journal-83233.20191219002821.7defe0cf7a381733@ruby-lang.org>
Issue #16039 has been updated by sawa (Tsuyoshi Sawada).


JustJosh (Joshua Stowers) wrote:
> I do not think we should use the name `cover?` because the types of arguments accepted by `Range#cover?` would be incompatible with this use case.
> 
> For example:
> ```ruby
> (1..3).cover?(2) # true
> ```
> But if Array's implementation worked similarly, we would have the following issue:
> ```ruby
> [1, 2, 3].cover?(2) # true by design of Range#cover?
> [1, 2, 3].cover?([2]) # true because all values in argument are also in self
> [1, [2], 3].cover?([2]) # ?
> ```

When the argument is an array, it should be understood as the usual case; i.e., it should be interpreted as the $\subset$ relation. Otherwise, it should be considered as the abbreviated form; in such case, it should be interpreted as the $\in$ relation. So

```ruby
[1, [2], 3].cover?([2])
```

should be unambiguously `false`. To achieve the interpretation that leads to the `true` output, you need to write:

```ruby
[1, [2], 3].cover?([[2])
```

That is exactly analogous to how `Range#cover` works, and there hasn't been a problem there.

----------------------------------------
Feature #16039: Array#contains? to check if one array contains another array
https://bugs.ruby-lang.org/issues/16039#change-83233

* Author: cha1tanya (Prathamesh Sonpatki)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I woud like to propose `Array#contains?` which will check if the one array is part of another array.
Implementation can be as follows:

```
def contains?(other)
  (other - self).empty?
end
```

Some test cases:

```
[1, 2, 3].contains?([2, 3]) => true
[1, 2, 3].contains?([]) => true
[1, 2, 3].contains?([1, 2, 3, 4]) => false
[].contains?([]) => true
[].contains?([1, 2, 3, 4]) => false
```



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