Skip to content

Instantly share code, notes, and snippets.

@ikataitsev
Created April 3, 2013 12:17
Show Gist options
  • Save ikataitsev/5300689 to your computer and use it in GitHub Desktop.
Save ikataitsev/5300689 to your computer and use it in GitHub Desktop.
counting q
class Activity < ActiveRecord::Base
# Activity#result is a boolean
end
# Let's say
Activity.all.pluck(:result) # => [false, true, false, false, false, true]
# How can I calculate the number of the false results before that true one?
@roy
Copy link

roy commented Apr 3, 2013

def failed_attempts(activities)
  activities = activities[0..-2] if activities.last == true

  return activities.length - activities.rindex(true) - 1
end

puts failed_attempts(Activity.all.pluck(:result))
# >> 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment