1
0
mirror of https://github.com/meineerde/rackstash.git synced 2025-12-19 15:01:12 +00:00

Add Rackstash::UNDEFINED constant

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.
This commit is contained in:
Holger Just 2017-07-11 23:15:38 +02:00
parent da1999dba3
commit d07a9452f4
2 changed files with 35 additions and 0 deletions

View File

@ -37,6 +37,29 @@ 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.
#
# 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
# @return [Boolean] `true` iff `other` is the exact same object as `self`
def ==(other)
self.equal?(other)
end
alias === ==
alias eql? ==
# @return [String] the string `"undefined"`
def to_s
'undefined'.freeze
end
alias inspect to_s
end
UNDEFINED = UndefinedClass.new
EMPTY_STRING = ''.freeze
EMPTY_SET = Set.new.freeze

View File

@ -41,4 +41,16 @@ describe Rackstash do
expect(Rackstash.const_get(name)).to be_frozen
end
end
it 'defines UNDEFINED' do
expect(Rackstash::UNDEFINED).to be_instance_of Rackstash::UndefinedClass
expect(Rackstash::UNDEFINED.to_s).to eql 'undefined'
expect(Rackstash::UNDEFINED).to equal Rackstash::UNDEFINED
expect(Rackstash::UNDEFINED).not_to eql nil
expect(Rackstash::UNDEFINED).not_to eql false
expect(Rackstash::UNDEFINED).not_to eql true
expect(Rackstash::UNDEFINED).not_to eql 42
end
end