I want to AND
or OR
all the elements in an array, but with some control, as shown via the hash element selection. Here is the behavior that I wish to achieve:
a = [{:a => true}, {:a => false}]
a.and_map{ |hash_element| hash_element[:a] }
#=> false
a.or_map{ |hash_element| hash_element[:a] }
#=> true
Is there a slick, clean way to do this in Ruby?
Boolean Operators are simple words (AND, OR, NOT or AND NOT) used as conjunctions to combine or exclude keywords in a search, resulting in more focused and productive results. This should save time and effort by eliminating inappropriate hits that must be scanned before discarding.
Boolean operators form the basis of mathematical sets and database logic. They connect your search words together to either narrow or broaden your set of results. The three basic boolean operators are: AND, OR, and NOT.
You can use all?
and any?
for that:
a = [{:a => true}, {:a => false }]
a.any? { |hash_element| hash_element[:a] }
#=> true
a.all? { |hash_element| hash_element[:a] }
#=> false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With