Skip to content

Instantly share code, notes, and snippets.

@mihar
Last active August 29, 2015 13:59
Show Gist options
  • Save mihar/10988181 to your computer and use it in GitHub Desktop.
Save mihar/10988181 to your computer and use it in GitHub Desktop.
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