Mastering Enumerable, Part 1

Flashcard 7 of 7

How can we simplify this?

# Returns true when all users are administrators, false
# otherwise.
def all_are_admins?(users)
  all_admins = true

  users.each do |user|
    all_admins = all_admins && user.admin?
  end

  all_admins
end
def all_are_admins?(users)
  users.all?(&:admin?)
end

You might also be interested all?'s cousin, any?.

Return to Flashcard Results