Issue #16146 has been updated by ngomez (Nancy Gomez).


As recommended I'll place in example directly here:

The feature I am requesting is to allow custom comparison of the Array #difference.
Currently it is comparing with each item's #eql and #hash but in the case of Objects you would need to redefine these to get the intended result.

In my case, I can not simply redefine these as getting the difference of the two arrays of Objects is one small portion of the overall logic, the objects I am using come from a third party which also uses the #eql and #hash for other logic I rely on. So I would have to set and somehow unset my definition (which not sure I can unset anyway) or I can just not use the #difference which seems like such a waste because it would be such an elegant and clear solution.


Here is an example of my desired usage, let's say I have the following class:

```
# Defined by a third party which uses #eql and #hash for other logic
class Num
  def initialize(val)
    @val = val
  end

  def val
    @val
  end
end
``` 

And I have the following arrays:

```
all = [Num.new(1), Num.new(2), Num.new(3)]
subset = [Num.new(1), Num.new(2)]
```

I want to find all the values that the `subset` is missing from `all`:

```
all.difference(subset)
=> [#<Num:0x00007fcae19e9540 @val=1>, #<Num:0x00007fcae19e9518 @val=2>, #<Num:0x00007fcae19e94f0 @val=3>]
``` 

But of course, #difference is comparing view #eql and #hash so I attempt to define how to compare:

````
all.difference(subset) { |a, b| a.val <=> b.val }
=> [#<Num:0x00007fcae19e9540 @val=1>, #<Num:0x00007fcae19e9518 @val=2>, #<Num:0x00007fcae19e94f0 @val=3>]
```

Still no luck, and I'm not 100% sure that the syntax is exactly correct anyway, so I take a look at the docs: https://ruby-doc.org/core-2.6/Array.html#method-i-difference

And notice no example shows custom comparison, and the implementation doesn't seem to support it either.

I would love it if instead this would be the result:

```
all.difference(subset) { |a, b| a.val <=> b.val }
=> [#<Num:0x00007fcae19e94f0 @val=3>]
```

Does that make sense?

----------------------------------------
Feature #16146: Array .difference allow custom comparison
https://bugs.ruby-lang.org/issues/16146#change-81484

* Author: ngomez (Nancy Gomez)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Hello!

I wanted to know if there's any plan to implement the ability to define comparison between individual items in arrays.

Here is where I originally asked the question on Stack Overflow: https://stackoverflow.com/questions/57316775/is-there-any-way-to-specify-how-to-compare-of-array-of-objects-for-difference-f

But specifically, I noticed nearly all the existing Array functions allow us to define ways for comparison, such as #uniq but #difference does not :( This is quite unfortunate but I'm hoping it's just because this function was introduced in 2.6.0, and hopefully this feature can be implemented?

Thanks for any and all info :)



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

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>