Class: Object

Inherits:
BasicObject
Defined in:
runtime/ext/object.rb,
runtime/ext/object.rbs

Overview

Object — the blank-value predicates, over every receiver.

One generic method answers for every receiver, so x.blank? cannot drift from y.blank? by the receiver's class.

Reopening Object relies on the pinned toolchain keeping dynamic Object.const_get intact (a store-backed job adapter resolves stored names that way; upstream 463242126 carries the fix).

Instance Method Summary collapse

  • #blank? ⇒ Boolean

    the generic emptiness predicate: nil, false, an empty Array/Hash, and a whitespace-only String are blank.

  • #presence ⇒ Object?

    the receiver when present, nil when blank: the x.presence || fallback shape replaces blank? ? nil : x at the call site.

  • #presence_in(list) ⇒ Object?

    the receiver when it is in the allow-list, nil otherwise: the gate for a value that came off the wire, with the list naming the allowed values.

  • #present? ⇒ Boolean

    the negation of blank?, for call sites that read better in the positive.

Instance Method Details

#blank? ⇒ Boolean

the generic emptiness predicate: nil, false, an empty Array/Hash, and a whitespace-only String are blank. Every other value is present: 0 and :sym are values, not emptiness, and a String is judged by its own text ("" and " " are blank).

Examples:

nil.blank?    # => true
" ".blank?    # => true
[].blank?     # => true
0.blank?      # => false

Returns:

  • (Boolean) —

    true when the receiver holds nothing



21
# File 'runtime/ext/object.rb', line 21

def blank?: () -> bool

#presence ⇒ Object?

the receiver when present, nil when blank: the x.presence || fallback shape replaces blank? ? nil : x at the call site.

Examples:

"x".presence  # => "x"
"".presence   # => nil

Returns:

  • (Object, nil) —

    the receiver itself, or nil when blank



46
# File 'runtime/ext/object.rb', line 46

def presence: () -> untyped

#presence_in(list) ⇒ Object?

the receiver when it is in the allow-list, nil otherwise: the gate for a value that came off the wire, with the list naming the allowed values. Membership compares the receiver's text, so a Symbol value matches its String spelling in both kinds of list; a non-Array list is rejected at the call — a String would silently substring-match.

Examples:

"member".presence_in(["member", "admin"])  # => "member"
"other".presence_in(["member", "admin"])   # => nil

Parameters:

Returns:

  • (Object, nil) —

    the receiver itself, or nil when not listed

Raises:

  • (ArgumentError) —

    the allow-list is not an Array



62
# File 'runtime/ext/object.rb', line 62

def presence_in: (Array[String] list) -> untyped

#present? ⇒ Boolean

the negation of blank?, for call sites that read better in the positive.

Examples:

"x".present?  # => true
[].present?   # => false

Returns:

  • (Boolean) —

    true when the receiver holds something



35
# File 'runtime/ext/object.rb', line 35

def present?: () -> bool