Set operations on array of hash

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.

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

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

Also, check out the follow-up reply.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs