Issue #16146 has been updated by jonathanhefner (Jonathan Hefner).


duerst (Martin D=FCrst) wrote:
> Well, that assumes that the block return values can be ordered. But it's =
easy to create cases where values can be compared for equality, but not ord=
ered. Example: `[1, 'abc']`.

I'm not sure that I follow.  Here is a naive implementation of what I was e=
nvisioning:

```ruby
def difference(*other_arrays, &block)
  if block
    rejected =3D other_arrays.flatten(1).map(&block).to_set
    self.reject{|element| rejected.include?(block.call(element)) }
  else
    # original implementation
  end
end
```

> Also, this proposal is about adding a block to `difference`, but if we do=
 that, we should add it to `union` and `intersection` and so on, too.

Agreed, that would probably be best for consistency.


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

* 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 com=
parison between individual items in arrays.

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

But specifically, I noticed nearly all the existing Array functions allow u=
s 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 wa=
s 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=3Dunsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>