Existence
Existence is a simple Ruby library that exposes Active Support's present? and blank? methods to the following
classes:
ArrayFalseClassHashNilClassStringTrueClass
It is intended to provide these helper methods to projects that do not include ActiveSupport. It is tested against
the latest patch release for each minor version of Ruby 2.
Installation
In your Gemfile, include the existence gem and then bundle install.
gem 'existence'Usage
The same patterns used in ActiveSupport can be expected with Existence.
-
An array is present if it has elements, and blank if it does not.
[].present? # => false [].blank? # => true ['test'].present? # => true ['test'].blank? # => false
-
falseis never present and always blank.false.present? # => false false.blank? # => true
-
A hash is present if it contains values, and blank if it does not.
{}.present? # => false {}.blank? # => true { test: true }.present? # => true { test: true }.blank? # => false
-
nilis never present and always blank.nil.present? # => false nil.blank? # => true
-
A string is present if it contains non-whitespace characters, and blank if it does not.
''.present? # => false ''.blank? # => true 'test'.present? # => true 'test'.blank? # => false
-
trueis always present and never blank.true.present? # => true true.blank? # => false