Class: Object

Inherits:
BasicObject
Defined in:
lib/core_ext/object.rb

Overview

Reopen the core Object class to add #identify to all objects.

Instance Method Summary collapse

Instance Method Details

#identify(*args) ⇒ String #identify(*args, options) ⇒ String

Instance method for constructing a self-identifying string for any given object or list of objects.

Examples:

OpenStruct.new(a: 1, b: '2', c: :"3").identify(:a, :b, :c)
# => "OpenStruct[a:1, b:\"2\", c::\"3\"]"

1.identify(:to_s) # => "Integer[to_s:\"1\"]"
nil.identify      # => "[no objects]"

%w(1 2).identify(:to_i, :to_f)
# => "String[to_i:1, to_f:1.0], String[to_i:2, to_f:2.0]"

(1..10).to_a.identify(:to_f, limit: 2)
# => "Integer[to_f:1.0], Integer[to_f:2.0], ... (8 more)"

Overloads:

  • #identify(*args) ⇒ String

    Parameters:

    • args (*)

      (optional) a list of arguments to identify for this object or for each object in this collection

  • #identify(*args, options) ⇒ String

    Parameters:

    • args (*)

      (optional) (default: :id) a list of arguments to identify for this object

    • options (Hash)

      the options for building a customized self-identifier

    Options Hash (options):

    • :klass (String, nil)

      object class name override

    • :limit (Integer)

      maximum number of objects to display from a collection

Returns:

  • (String)

    a self-identifying string



34
35
36
# File 'lib/core_ext/object.rb', line 34

def identify(*args, **kwargs)
  ObjectIdentifier.(self, *args, **kwargs)
end