Class: Object

Inherits:
BasicObject
Defined in:
lib/vagrant-skytap/core_ext/try.rb,
lib/vagrant-skytap/core_ext/object/tap.rb,
lib/vagrant-skytap/core_ext/object/blank.rb

Overview

Copied from Rails ActiveSupport 2.3.18: activesupport-2.3.18/lib/active_support/core_ext/object/blank.rb

License

Active Support is released under the MIT license.

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

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

This simplifies:

if !address.nil? && !address.empty?

…to:

if !address.blank?

Returns:

  • (Boolean)


18
19
20
# File 'lib/vagrant-skytap/core_ext/object/blank.rb', line 18

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

#presenceObject

Returns object if it’s #present? otherwise returns nil. object.presence is equivalent to object.present? ? object : nil.

This is handy for any representation of objects where blank is the same as not present at all. For example, this simplifies a common check for HTTP POST/query parameters:

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'


41
42
43
# File 'lib/vagrant-skytap/core_ext/object/blank.rb', line 41

def presence
  self if present?
end

#present?Boolean

An object is present if it’s not blank.

Returns:

  • (Boolean)


23
24
25
# File 'lib/vagrant-skytap/core_ext/object/blank.rb', line 23

def present?
  !blank?
end

#tap {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Object)

    the object that the method was called on



3
4
5
6
# File 'lib/vagrant-skytap/core_ext/object/tap.rb', line 3

def tap
  yield self
  self
end