Class: Object Private

Inherits:
BasicObject
Defined in:
extend/blank.rb,
extend/object/deep_dup.rb,
extend/object/duplicable.rb

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Most objects are cloneable, but not all. For example you can't dup methods:

method(:puts).dup # => TypeError: allocator undefined for Method

Classes may signal their instances are not duplicable removing +dup+/+clone+ or raising exceptions from them. So, to dup an arbitrary object you normally use an optimistic approach and are ready to catch an exception, say:

arbitrary_object.dup rescue object

Rails dups objects in a few critical spots where they are not that arbitrary. That rescue is very expensive (like 40 times slower than a predicate) and it is often triggered.

That's why we hardcode the following cases and check duplicable? instead of using that rescue idiom.

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

An object is blank if it's false, empty, or a whitespace string.

For example, nil, '', ' ', [], {} and false are all blank.

Example

!address || address.empty?

can be simplified to

address.blank?

Returns:

  • (Boolean)


21
22
23
# File 'extend/blank.rb', line 21

def blank?
  respond_to?(:empty?) ? !!T.unsafe(self).empty? : false
end

#deep_dupT.self_type

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns a deep copy of object if it's duplicable. If it's not duplicable, returns self.

object = Object.new
dup    = object.deep_dup
dup.instance_variable_set(:@a, 1)

object.instance_variable_defined?(:@a) # => false
dup.instance_variable_defined?(:@a)    # => true

Returns:

  • (T.self_type)


17
18
19
# File 'extend/object/deep_dup.rb', line 17

def deep_dup
  duplicable? ? dup : self
end

#duplicable?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Can you safely dup this object?

False for method objects; true otherwise.

Returns:

  • (Boolean)


30
# File 'extend/object/duplicable.rb', line 30

def duplicable? = true

#presenceT.self_type?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns the receiver if it's present, otherwise returns nil.

object.presence is equivalent to object.present? ? object : nil.

Example

state   = params[:state]   if params[:state].present?
country = params[:country] if params[:country].present?
region  = state || country || 'US'

can be simplified to

region = params[:state].presence || params[:country].presence || 'US'

Returns:

  • (T.self_type, nil)


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

def presence
  self if present?
end

#present?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

An object is present if it's not blank.

Returns:

  • (Boolean)


27
# File 'extend/blank.rb', line 27

def present? = !blank?