Class: None

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

Overview

The simplest NullObject there can be

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(methods) ⇒ None

Returns a new instance of None.



22
23
24
# File 'lib/deterministic/maybe.rb', line 22

def initialize(methods)
  @methods = methods
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



35
36
37
38
# File 'lib/deterministic/maybe.rb', line 35

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

Class Method Details

.instanceObject



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

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

.method_missing(m, *args) ⇒ Object



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

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

.mimic(klas) ⇒ Object



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

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

Instance Method Details

#none?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/deterministic/maybe.rb', line 40

def none?
  true
end

#respond_to?(m) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/deterministic/maybe.rb', line 48

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

#some?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/deterministic/maybe.rb', line 44

def some?
  false
end

#to_aryObject



31
32
33
# File 'lib/deterministic/maybe.rb', line 31

def to_ary
  []
end

#to_strObject

implicit conversions



27
28
29
# File 'lib/deterministic/maybe.rb', line 27

def to_str
  ''
end