Class: Null

Inherits:
Object show all
Defined in:
lib/deterministic/null.rb

Overview

The simplest NullObject there can be

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(methods) ⇒ Null

Returns a new instance of Null.



34
35
36
# File 'lib/deterministic/null.rb', line 34

def initialize(methods)
  @methods = methods
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



47
48
49
50
# File 'lib/deterministic/null.rb', line 47

def method_missing(m, *args)
  return self if respond_to?(m)
  super
end

Class Method Details

.==(other) ⇒ Object



28
29
30
# File 'lib/deterministic/null.rb', line 28

def ==(other)
  other.respond_to?(:null?) && other.null?
end

.instanceObject



12
13
14
# File 'lib/deterministic/null.rb', line 12

def instance
  @instance ||= new([])
end

.method_missing(m, *args) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/deterministic/null.rb', line 4

def method_missing(m, *args)
  if m == :new
    super
  else
    Null.instance
  end
end

.mimic(klas) ⇒ Object



24
25
26
# File 'lib/deterministic/null.rb', line 24

def mimic(klas)
  new(klas.instance_methods(false))
end

.null?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/deterministic/null.rb', line 16

def null?
  true
end

.some?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/deterministic/null.rb', line 20

def some?
  false
end

Instance Method Details

#==(other) ⇒ Object



69
70
71
# File 'lib/deterministic/null.rb', line 69

def ==(other)
  other.respond_to?(:null?) && other.null?
end

#inspectObject



65
66
67
# File 'lib/deterministic/null.rb', line 65

def inspect
  'Null'
end

#null?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/deterministic/null.rb', line 52

def null?
  true
end

#respond_to?(m) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/deterministic/null.rb', line 60

def respond_to?(m)
  return true if @methods.empty? || @methods.include?(m)
  super
end

#some?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/deterministic/null.rb', line 56

def some?
  false
end

#to_aryObject



43
44
45
# File 'lib/deterministic/null.rb', line 43

def to_ary
  []
end

#to_strObject

implicit conversions



39
40
41
# File 'lib/deterministic/null.rb', line 39

def to_str
  ''
end