Consider you wanna make sure a string is not empty as well as not nil. If you are using rails you have a blank? method to do it. but what if you are not on rails?
As usual you will write a method something like
But this will through error undefined method empty?’ for nil:NilClass when you pass a
nil to this method, since
nil doesn’t have a method called
So I recommend you to test
nil? first, like
value.nil? will return
true and ruby won’t check the second part since
ruby uses Short Circuit Evaluation
You can also convert the value to string before checking empty, so that you don’t wanna always follow this order.
This will work since
nil.to_s will return an empty string.
If you pass
false as boolean this method will return false, since
"false".empty? will be false. If you are expecting other datatypes to this method then you will definity need more logic.
Happy coding. ;)