Sometimes having too many good things is not really good for you. So many ways to test for existence of the object or array that it's confusing.
Here is a short version:
- nil?
Works on any object and checks is it nil or not.
It returns true only for nil value.
- empty?
It does work on strings, hashes and arrays, and enumerators in general.
It returns true if there are not elements in the array.
It does not handle nil values, you have to check is object is nil and is object is empty.
a.nil? && a.empty?
- blank?
It is Rails construct, not part of Ruby.
It is a shortcut for false, empty of whitespace string.
- present?
Opposite of blank, part of Rails framework.
- any?
It works on arrays, hashes and sets.
It checks is there is anything in the collection that can evaluate to true?
It returns true if array has no elements except when it has nil values only.
Here is a short version:
- nil?
Works on any object and checks is it nil or not.
It returns true only for nil value.
- empty?
It does work on strings, hashes and arrays, and enumerators in general.
It returns true if there are not elements in the array.
It does not handle nil values, you have to check is object is nil and is object is empty.
a.nil? && a.empty?
- blank?
It is Rails construct, not part of Ruby.
It is a shortcut for false, empty of whitespace string.
- present?
Opposite of blank, part of Rails framework.
- any?
It works on arrays, hashes and sets.
It checks is there is anything in the collection that can evaluate to true?
It returns true if array has no elements except when it has nil values only.
No comments:
Post a Comment