Class: Object

Inherits:
BasicObject
Defined in:
lib/activesupport/blank.rb

Overview

Downloaded from raw.githubusercontent.com/rails/rails/v6.1.3.1/activesupport/lib/active_support/core_ext/object/blank.rb Slight modification on String#blank?: encoding error means false for simplicity

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)


20
21
22
# File 'lib/activesupport/blank.rb', line 20

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:



47
48
49
# File 'lib/activesupport/blank.rb', line 47

def presence
  self if present?
end

#present?true, false

An object is present if it’s not blank.

Returns:

  • (true, false)


27
28
29
# File 'lib/activesupport/blank.rb', line 27

def present?
  !blank?
end