Class: Object

Inherits:
BasicObject
Defined in:
lib/camper/core_extensions/object.rb

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)


19
20
21
# File 'lib/camper/core_extensions/object.rb', line 19

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:



46
47
48
# File 'lib/camper/core_extensions/object.rb', line 46

def presence
  self if present?
end

#present?true, false

An object is present if it’s not blank.

Returns:

  • (true, false)


26
27
28
# File 'lib/camper/core_extensions/object.rb', line 26

def present?
  !blank?
end