Class: Object

Inherits:
BasicObject
Defined in:
lib/reading/util/blank.rb

Overview

Copied from active_support/core_ext/object/blank github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/object/blank.rb Except detection of ENCODED_BLANKS is omitted here.

Instance Method Summary collapse

Instance Method Details

#blank?true, false

An object is blank if it’s false, empty, or a whitespace string. For example, nil, ”, ‘ ’, [], {}, and false are all blank.

This simplifies

!address || address.empty?

to

address.blank?

Returns:

  • (true, false)


17
18
19
# File 'lib/reading/util/blank.rb', line 17

def blank?
  respond_to?(:empty?) ? !!empty? : !self
end

#presenceObject

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'

Returns:



44
45
46
# File 'lib/reading/util/blank.rb', line 44

def presence
  self if present?
end

#present?true, false

An object is present if it’s not blank.

Returns:

  • (true, false)


24
25
26
# File 'lib/reading/util/blank.rb', line 24

def present?
  !blank?
end