Class: Object Private
- Inherits:
- BasicObject
- Extended by:
- MacOSVersionErrorCompat
- Defined in:
- extend/blank.rb,
macos_version.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
-
#blank? ⇒ Boolean
private
An object is blank if it’s false, empty, or a whitespace string.
-
#deep_dup ⇒ T.self_type
private
Returns a deep copy of object if it’s duplicable.
-
#duplicable? ⇒ Boolean
private
Can you safely dup this object?.
-
#presence ⇒ T.self_type?
private
Returns the receiver if it’s present otherwise returns +nil+.
-
#present? ⇒ Boolean
private
An object is present if it’s not blank.
Methods included from MacOSVersionErrorCompat
Methods included from Kernel
#disk_usage_readable, #ensure_executable!, #ensure_formula_installed!, #exec_browser, #exec_editor, #ignore_interrupts, #interactive_shell, #number_readable, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #paths, #pretty_duration, #pretty_installed, #pretty_outdated, #pretty_uninstalled, #quiet_system, #redact_secrets, #redirect_stdout, #require?, #safe_system, #tap_and_name_comparison, #truncate_text_to_approximate_size, #which, #which_all, #which_editor, #with_custom_locale, #with_env, #with_homebrew_path
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.
This simplifies
!address | address.empty? |
to
address.blank?
16 17 18 |
# File 'extend/blank.rb', line 16 def blank? respond_to?(:empty?) ? !!T.unsafe(self).empty? : false end |
#deep_dup ⇒ T.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
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.
28 |
# File 'extend/object/duplicable.rb', line 28 def duplicable? = true |
#presence ⇒ T.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
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’ |
39 40 41 |
# File 'extend/blank.rb', line 39 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.
22 |
# File 'extend/blank.rb', line 22 def present? = !blank? |