Class: Toys::UniqueKey

Inherits:
Object
  • Object
show all
Defined in:
lib/toys/unique_key.rb

Overview

A named unique key, useful for Context keys or sentinels.

Each individual instance of this class is unique and not equal to any other instance, even if they have the same user-visible name.

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ UniqueKey

Create a new UniqueKey.

Parameters:

  • name (String, nil) (defaults to: nil)

    Optional name. If not passed, a default is constructed using the object ID.



17
18
19
20
21
22
# File 'lib/toys/unique_key.rb', line 17

def initialize(name = nil)
  # Interpolated literals are frozen on Ruby 2.7 but not on 3.0+, so this
  # freeze is required despite what RuboCop's 2.7 target thinks.
  @name = name ? -name : "(unnamed unique key #{object_id})".freeze # rubocop:disable Style/RedundantFreeze
  freeze
end

Instance Method Details

#inspectString

Returns a user-visible name of this key.

Returns:

  • (String)

    a user-visible name of this key



34
35
36
# File 'lib/toys/unique_key.rb', line 34

def inspect
  @name
end

#to_sString

Returns a user-visible name of this key.

Returns:

  • (String)

    a user-visible name of this key



27
28
29
# File 'lib/toys/unique_key.rb', line 27

def to_s
  @name
end