Skip to content

Instantly share code, notes, and snippets.

@davidteren
Created June 7, 2022 18:19
Show Gist options
  • Save davidteren/46a7649c27b91d92b98c65112cd0eea0 to your computer and use it in GitHub Desktop.
Save davidteren/46a7649c27b91d92b98c65112cd0eea0 to your computer and use it in GitHub Desktop.
A good way to validate whether an object is Truthy or Falsey in Ruby or Rails apps is to use the ActiveSupport present? & blank? methods.
# The Rails ActiveSupport core extensions provide additional
# functionality to any Rails or Ruby application.
require "active_support"
# ActiveSupport#blank?
nil.blank? # => true
false.blank? # => true
{}.blank? # => true
[].blank? # => true
"".blank? # => true
" ".blank? # => true
# ActiveSupport#present?
nil.present? # => false
false.present? # => false
{}.present? # => false
[].present? # => false
"".present? # => false
" ".present? # => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment