Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/blank.rb
Instance Method Summary collapse
-
#blank? ⇒ true, false
An object is blank if it's false, empty, or a whitespace string.
-
#presence ⇒ Object
Returns the receiver if it's present otherwise returns +nil+.
-
#present? ⇒ true, false
An object is present if it's not blank.
Instance Method Details
#blank? ⇒ true, false
An object is blank if it's false, empty, or a whitespace string. For example, '', ' ', +nil+, [], and {} are all blank.
This simplifies
address.nil? || address.empty?
to
address.blank?
16 17 18 |
# File 'lib/blank.rb', line 16 def blank? respond_to?(:empty?) ? !!empty? : !self end |
#presence ⇒ Object
Returns the receiver if it's present otherwise returns +nil+. object.presence is equivalent to
object.present? ? object : nil
For example, something like
state = params[:state] if params[:state].present? country = params[:country] if params[:country].present? region = state || country || 'US'
becomes
region = params[:state].presence || params[:country].presence || 'US'
43 44 45 |
# File 'lib/blank.rb', line 43 def presence self if present? end |
#present? ⇒ true, false
An object is present if it's not blank.
23 24 25 |
# File 'lib/blank.rb', line 23 def present? !blank? end |