Skip to content

Instantly share code, notes, and snippets.

@wuputah
Created December 24, 2008 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wuputah/39763 to your computer and use it in GitHub Desktop.
Save wuputah/39763 to your computer and use it in GitHub Desktop.
# All the places I used !! in a very sizable project
# (4 cases in 7600 LOC, about 40% of which is specs)
# ActiveRecord callback - writing to a DB:
def update_has_emailable_accounts!
# !! replaces "condition ? true : false"
self.update_attribute(:has_emailable_accounts, !!accounts.detect { |account| account.emailable? })
end
# AR validation helper predicate:
def valid_url?(url)
!! (url =~ %r[^https?://.]i)
end
# application.rb - user? predicate:
def user?
!! current_user
end
helper_method :user?
# In a custom validation I wrote - returns true/false instead of... true/nil.
# I'd only justify this because its a predicate, but this is certainly
# my least solid usage.
def has_uniqueness_errors?
!! @has_uniqueness_errors
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment