Welcome...



...All those moments will be lost in time, like tears in rain....

- soliloquy from Blade Runner


Bits and bytes do get lost, awash in the rain of data flow that is Internet. They slip away from us, never to found again... some of them will be captured here, many more will not... like tears in rain...



Monday, October 29, 2018

Ruby: blank vs empty vs any vs nil vs...

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.

No comments:

Post a Comment