# Someone please name this matcher for me

**URL:** <https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199>\
**Category:** RSpec\
**Created:** [August 6, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199 "2008-08-06T19:50:48Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 6, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199/1 "2008-08-06T19:50:48Z")

</div>

I’ve had a matcher in my head for a couple months, that I frequently  
want but never get around to writing because I can’t think of the name  
for it. Here’s how it would look

[1, 2, 3, 4, 1].should … [1, 3, 1, 4, 2]

I have a couple ideas for names, but I’ll hold off on them so I don’t  
influence anyone’s opinion.

btw, I realize that .sort works in this case. But that’s slightly  
less clear than I’d like, and it doesn’t work for non-Comparable  
objects (like activerecords).

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 6, 2008, 8:07pm UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199/2 "2008-08-06T20:07:12Z")

</div>

perhaps verbose but how about

foo.should equal\_in\_any\_order [1, 3, 1, 4, 2]

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 6, 2008, 8:15pm UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199/3 "2008-08-06T20:15:25Z")

</div>

On Wed, Aug 6, 2008 at 1:50 PM, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> less clear than I’d like, and it doesn’t work for non-Comparable  
> objects (like activerecords).

[1, 2, 3, 4, 1].should consist\_of([1, 3, 1, 4, 2])

Since consist means “to be composed or made up of”, which matches the  
usage.

To me it reads like, this thing should be made up of these other things.

WDYT?

–  
Zach D.  
[http://www.continuousthinking.com](http://www.continuousthinking.com)

> **[Homepage | Mutually Human](https://www.mutuallyhuman.com)**
>
> We empower businesses to overcome challenges with a holistic focus on People, Process, and Technology for lasting results and innovation.

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 6, 2008, 8:15pm UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199/4 "2008-08-06T20:15:25Z")

</div>

On Wed, Aug 6, 2008 at 12:50 PM, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve had a matcher in my head for a couple months, that I frequently  
> want but never get around to writing because I can’t think of the name  
> for it. Here’s how it would look
> 
> [1, 2, 3, 4, 1].should … [1, 3, 1, 4, 2]

[1, 2, 3, 4, 1].should include\_all [1, 3, 1, 4, 2]

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 6, 2008, 8:42pm UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199/5 "2008-08-06T20:42:22Z")

</div>

Consider:  
should\_have\_the\_same\_members\_as

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 6, 2008, 8:59pm UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199/6 "2008-08-06T20:59:34Z")

</div>

How about  
[1, 2, 3, 4, 1].should contain([1, 3, 1, 4, 2])

[1, 2, 3, 4, 1].should contain\_only([1, 3, 1, 4, 2])

or (riffing off Zach)

[1, 2, 3, 4, 1].should be\_composed\_from([1, 3, 1, 4, 2])

Matt

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 6, 2008, 9:04pm UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199/7 "2008-08-06T21:04:38Z")

</div>

.should be\_bag\_of( [1, 3, 1, 4, 2] )

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 6, 2008, 8:45pm UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199/8 "2008-08-06T20:45:53Z")

</div>

On Wed, 6 Aug 2008 13:50:27 -0400, you wrote:

> I’ve had a matcher in my head for a couple months, that I frequently  
> want but never get around to writing because I can’t think of the name  
> for it. Here’s how it would look
> 
> [1, 2, 3, 4, 1].should … [1, 3, 1, 4, 2]

An unordered collection with possibly multiple occurrences of its  
members is known in mathematics as a “multiset,” or, informally, a  
“bag.”

-Steve

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 6, 2008, 9:25pm UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199/9 "2008-08-06T21:25:42Z")

</div>

2008-08-06 13:11, Mike V.:

> > [1, 2, 3, 4, 1].should … [1, 3, 1, 4, 2]  
> > [1, 2, 3, 4, 1].should include\_all [1, 3, 1, 4, 2]

I’d then think that

[1, 1, 1, 2, 3, 4].should include\_all [1, 3, 1, 4, 2]  
[1, 2, 3, 4, 5, 6].should include\_all [1, 3, 1, 4, 2]

Which afaik was not what Pat had in mind. Or was it?

And include\_all\_and\_nothing\_but is a bit verbose. 🙂

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 6, 2008, 9:09pm UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199/10 "2008-08-06T21:09:20Z")

</div>

2008-08-06 14:33, Steve Schafer:

> An unordered collection with possibly multiple occurrences of its  
> members is known in mathematics as a “multiset,” or, informally, a  
> “bag.”

I (standing knee-deep in mathematics) was about to offer the following

be\_multiset\_equal\_to  
be\_set\_equal\_to

Set equality is slightly (imo not fatally) incorrect, and multiset  
might be alien to majority of hackers out there. I’d prefer either of  
these or modifications.

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 7, 2008, 2:46am UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199/11 "2008-08-07T02:46:56Z")

</div>

On 07/08/2008, at 05:04 , Jonathan L. wrote:

> .should be\_bag\_of( [1, 3, 1, 4, 2] )

FWIW, I am in favour of this naming. Now back to lurking 🙂

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 7, 2008, 5:25pm UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199/12 "2008-08-07T17:25:52Z")

</div>

On Wed, Aug 6, 2008 at 1:25 PM, Cohen, Joseph [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Consider:  
> should\_have\_the\_same\_members\_as

Similarly, I have a have\_same\_elements\_as(arr) matcher.

k

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 7, 2008, 7:41pm UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199/13 "2008-08-07T19:41:13Z")

</div>

Lots of good, interesting suggestions.  
If you don’t mind its length, for the non-mathematically inclined  
something like

has\_same\_members\_as

may be easier to remember and corresponds to Ruby’s member? predicate.

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 8, 2008, 6:29pm UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199/14 "2008-08-08T18:29:29Z")

</div>

i wrote this matcher myself for a project i’m working on and have been  
calling it `equals\_without\_regard\_to\_order’. it’s an unwieldy name  
and i’ve never really been comfortable with it but i’ve continued to  
use it, mostly because i’m dim.

very curious to find a winning replacement on this list!

–  
Dhruv Bansal  
[http://www.infochimps.org](http://www.infochimps.org) || [http://www.dhruvbansal.com](http://www.dhruvbansal.com) ||  
removed\_email\_address@domain.invalid

“Mark W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 8, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199/15 "2008-08-08T22:16:40Z")

</div>

Zach D. wrote:

> [1, 2, 3, 4, 1].should consist\_of([1, 3, 1, 4, 2])

+1

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 11, 2008, 4:24pm UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199/16 "2008-08-11T16:24:09Z")

</div>

Would such a matcher also work in this instance:

Car.should\_receive(:find).with(bag\_of[@ford.id, @fiat.id])

Such that if I don’t know (or care) what order the IDs will be passed to  
the find method, this will pass?

Or is there already some other way to achieve that?

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 8, 2008, 6:08pm UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199/17 "2008-08-08T18:08:25Z")

</div>

is\_permutation\_of would be technically correct, but it doesn’t really  
express equality to me, which is the intent of the match.

///ark

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 11, 2008, 9:23pm UTC](https://www.ruby-forum.com/t/someone-please-name-this-matcher-for-me/145199/18 "2008-08-11T21:23:09Z")

</div>

On 07/08/2008, at 05:04 , Jonathan L. wrote:

> .should be\_bag\_of( [1, 3, 1, 4, 2] )

+1
