Created
June 7, 2022 18:19
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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