\$\begingroup\$
\$\endgroup\$
1
I've written a script that deletes a member of a hash if an array within the has contains all the other elements of another array within the same hash.
It works but it looks a little messy. Is there a better way to do this?
def title_contains_blocks(candidate, titles, index)
test_group = titles.dup
test_group.delete_at(index)
test_group.each do |title|
return true if true === title[:blocks].all?{|block| candidate[:blocks].include?(block)}
end
return false
end
titles = [
{:blocks => [1,2,3,4,5,6]},
{:blocks => [1,2,3]},
{:blocks => [4,5,6]}
]
titles = titles.delete_if.each_with_index {|candidate, index|
title_contains_blocks(candidate, titles, index)
}
puts titles.inspect
#=> [{:blocks=>[1, 2, 3]}, {:blocks=>[4, 5, 6]}]
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Apr 29, 2012 at 7:52
BenBen
-
\$\begingroup\$ Would it have been equivalent if you removed the [1,2,3] and [4,5,6]? That is, do you simply need to cover all digits and remove redundant entries? \$\endgroup\$Mark Thomas– Mark Thomas2012年04月29日 12:36:48 +00:00Commented Apr 29, 2012 at 12:36
1 Answer 1
\$\begingroup\$
\$\endgroup\$
1
Convert the Hashes to Arrays, then use the difference operations on them.
a = { :a => 1, :b => 2, :c => 2 }
b = { :a => 1 }
p (b.to_a - a.to_a).empty?
You can do other things, like identify if two Hashes overlap in any places, by use the intersection too:
p (a.to_a & b.to_a).any?
-
\$\begingroup\$ in the the first example, if
b
is a superset ofa
then it would also result inempty? == true
... FYI for everybody trying to compare with this method \$\endgroup\$akostadinov– akostadinov2015年01月16日 07:11:58 +00:00Commented Jan 16, 2015 at 7:11
lang-rb