Last active
November 2, 2015 11:23
-
-
Save yyalim/91d3bdac7d4b5d300de7 to your computer and use it in GitHub Desktop.
Trick to handle no method error on nil objects
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
"hello world".upcase | |
# => "HELLO WORLD" | |
nil.upcase | |
# NoMethodError: undefined method `upcase' for nil:NilClass | |
# "gem install activesupport" if it is not installed | |
require 'active_support/core_ext/object/blank' | |
nil.presence && nil.upcase | |
# => nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment