# Set operations on array of hash

**URL:** <https://www.ruby-forum.com/t/set-operations-on-array-of-hash/126141>\
**Category:** Ruby\
**Created:** [January 15, 2008, 8:15pm UTC](https://www.ruby-forum.com/t/set-operations-on-array-of-hash/126141 "2008-01-15T20:15:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 15, 2008, 8:15pm UTC](https://www.ruby-forum.com/t/set-operations-on-array-of-hash/126141/1 "2008-01-15T20:15:59Z")

</div>

var1 = [  
{‘color’=\>‘blue’,‘shape’=\>‘round’,‘flavor’=\>‘sweet’}, ## 1a  
{‘color’=\>‘yellow’,‘shape’=\>‘oblong’,‘flavor’=\>‘sweet’},## 1b  
{‘color’=\>‘red’,‘shape’=\>‘round’,‘flavor’=\>‘tart’}, ## 1c  
];

var2 = [  
{‘color’=\>‘orange’,‘shape’=\>‘round’,‘flavor’=\>‘tangy’}, ## 2a  
{‘color’=\>‘red’,‘shape’=\>‘round’,‘flavor’=\>‘tart’}, ## 2c  
];

puts var1.union(var2); ## (1a…2a)  
puts var1.intersect(var2); ## (1c)  
puts var1.notin(var2); ## (1a,1b)

Assuming you have two variables v1 and v2 declared as above,  
is there a way to use the set operations of ruby to do set operations  
where the elements of the “set” are each a hash?

The assumption is two elements are equal iff they both have the  
exact same keys and the exact same values for those keys.

The naive approach of just using “require set” and applying ordinary  
set operations doesn’t seem to work.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 16, 2008, 3:26am UTC](https://www.ruby-forum.com/t/set-operations-on-array-of-hash/126141/2 "2008-01-16T03:26:01Z")

</div>

I know this doesn’t directly answer your question, but see:

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/51362](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/51362)

Also, check out the follow-up reply.
