1
0
mirror of https://github.com/meineerde/rackstash.git synced 2026-02-01 01:37:12 +00:00

Define UndefinedClass as an actual Singleton

This takes care of edge-cases and ensures that multiple loads of
`lib/rackstash.rb` do not fail on an attempted second initialization of
the UndefinedClass.
This commit is contained in:
Holger Just 2018-06-21 13:56:47 +02:00
parent a807f99af5
commit fa1abb5ab5

View File

@ -6,6 +6,7 @@
# of the MIT license. See the LICENSE.txt file for details.
require 'set'
require 'singleton'
require 'rackstash/version'
@ -86,13 +87,15 @@ module Rackstash
PROGNAME = "rackstash/v#{Rackstash::VERSION}".freeze
# A class for the {UNDEFINED} object. Generally, there will only be exactly
# one object of this class.
# A class for the {UNDEFINED} object. Being a singleton, there will only be
# exactly one object of this class: the {UNDEFINED} object.
#
# The {UNDEFINED} object can be used as the default value for method arguments
# to distinguish it from `nil`. See https://holgerjust.de/2016/detecting-default-arguments-in-ruby/#special-default-value
# for details.
class UndefinedClass
include ::Singleton
# @return [Boolean] `true` iff `other` is the exact same object as `self`
def ==(other)
self.equal?(other)
@ -107,12 +110,7 @@ module Rackstash
alias inspect to_s
end
UNDEFINED = UndefinedClass.new.tap do |undefined|
class << undefined.class
undef_method :allocate
undef_method :new
end
end
UNDEFINED = UndefinedClass.instance
EMPTY_STRING = ''.freeze
EMPTY_SET = Set.new.freeze