Class: Object

Inherits:
BasicObject
Includes:
Kernel
Defined in:
lib/buff/extensions/object/try.rb,
lib/buff/extensions/kernel.rb,
lib/buff/extensions/object/blank.rb

Overview

Instance Method Summary collapse

Methods included from Buff::Extensions::Kernel::Reporting

#capture, #enable_warnings, #quietly, #silence_stderr, #silence_stream, #silence_warnings, #suppress, #with_warnings

Instance Method Details

#blank?Boolean

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

This simplifies:

if address.nil? || address.empty?

…to:

if address.blank?

Returns:

  • (Boolean)

15
16
17
# File 'lib/buff/extensions/object/blank.rb', line 15

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'

38
39
40
# File 'lib/buff/extensions/object/blank.rb', line 38

def presence
  self if present?
end

#present?Boolean

An object is present if it's not blank?.

Returns:

  • (Boolean)

20
21
22
# File 'lib/buff/extensions/object/blank.rb', line 20

def present?
  !blank?
end

#try(*a, &b) ⇒ Object

Invokes the public method whose name goes as first argument just like public_send does, except that if the receiver does not respond to it the call returns nil rather than raising an exception.

This method is defined to be able to write

@person.try(:name)

instead of

@person ? @person.name : nil

try returns nil when called on nil regardless of whether it responds to the method:

nil.try(:to_i) # => nil, rather than 0

Arguments and blocks are forwarded to the method if invoked:

@posts.try(:each_slice, 2) do |a, b|
  ...
end

The number of arguments in the signature must match. If the object responds to the method the call is attempted and ArgumentError is still raised otherwise.

If try is called without arguments it yields the receiver to a given block unless it is nil:

@person.try do |p|
  ...
end

Please also note that try is defined on Object, therefore it won't work with instances of classes that do not have Object among their ancestors, like direct subclasses of BasicObject. For example, using try with SimpleDelegator will delegate try to the target instead of calling it on delegator itself.


44
45
46
47
48
49
50
# File 'lib/buff/extensions/object/try.rb', line 44

def try(*a, &b)
  if a.empty? && block_given?
    yield self
  else
    public_send(*a, &b) if respond_to?(a.first)
  end
end

#try!(*a, &b) ⇒ Object

Same as #try, but will raise a NoMethodError exception if the receiving is not nil and does not implemented the tried method.


54
55
56
57
58
59
60
# File 'lib/buff/extensions/object/try.rb', line 54

def try!(*a, &b)
  if a.empty? && block_given?
    yield self
  else
    public_send(*a, &b)
  end
end