Create a gist now

Instantly share code, notes, and snippets.

@mihar /gist:10988181
Last active Aug 29, 2015

What would you like to do?
Figure out if all booleans are true
array.reduce(true) { |x, y| x and y }

Useful for checking in Rails if a collection of models all have a flag set to true.

E.g.:

signers.map(&:ready?).reduce(true) { |x, y| x and y }

More examples:

irb(main):021:0> [true, true].reduce(true) { |x, y| x and y }
=> true

irb(main):022:0> [true, false].reduce(true) { |x, y| x and y }
=> false

irb(main):023:0> [true, false, true].reduce(true) { |x, y| x and y }
=> false

irb(main):024:0> [true, false, true, true].reduce(true) { |x, y| x and y }
=> false

irb(main):025:0> [true, true, true, true].reduce(true) { |x, y| x and y }
=> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment