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

There can be only one... UNDEFINED

By ensuring that we only ever create a single UNDEFINED object from the
UndefinedClass, it behaves more like nil
This commit is contained in:
Holger Just 2017-09-07 21:18:01 +02:00
parent 32e755396b
commit 74242caf0b
2 changed files with 24 additions and 9 deletions

View File

@ -84,7 +84,12 @@ module Rackstash
alias inspect to_s
end
UNDEFINED = UndefinedClass.new
UNDEFINED = UndefinedClass.new.tap do |undefined|
class << undefined.class
undef_method :allocate
undef_method :new
end
end
EMPTY_STRING = ''.freeze
EMPTY_SET = Set.new.freeze

View File

@ -51,16 +51,26 @@ describe Rackstash do
end
end
it 'defines UNDEFINED' do
expect(Rackstash::UNDEFINED).to be_instance_of Rackstash::UndefinedClass
expect(Rackstash::UNDEFINED.to_s).to eql 'undefined'
describe 'UNDEFINED' do
it 'defines the UndefinedClass' do
expect(Rackstash::UndefinedClass).to be_a Class
expect(Rackstash::UNDEFINED).to equal Rackstash::UNDEFINED
# No (further) ibjects can be created of this class
expect { Rackstash::UndefinedClass.new }.to raise_error NoMethodError
expect { Rackstash::UndefinedClass.allocate }.to raise_error NoMethodError
end
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
it 'defines the singleton object' 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
describe '.severity_label' do