Class: Blank

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/wrapped/blank.rb

Overview

The class represents a lack of something.

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object

Is this wrapped value equal to the given wrapped value? All blank values are equal to each other.

> nil.wrapped == nil.wrapped > 1.wrapped == nil.wrapped



96
97
98
# File 'lib/wrapped/blank.rb', line 96

def ==(other)
  other.is_a?(Blank)
end

#blank(&block) ⇒ Object

Call the block then return itself. This is chainable. See present for its companion.

w.blank { puts “I got nothing” }.present {|n| puts “got #{n}” }



33
34
35
36
# File 'lib/wrapped/blank.rb', line 33

def blank(&block)
  block.call
  self
end

#blank?Boolean

True; this is an instance of nothing.

Returns:

  • (Boolean)


70
71
72
# File 'lib/wrapped/blank.rb', line 70

def blank?
  true
end

#eachObject

Produce the empty list.

This class mixes in the Enumerable module, which relies on this.

> w.each {|n| puts n }



43
44
45
# File 'lib/wrapped/blank.rb', line 43

def each
  self
end

#flat_mapObject

Do nothing, returning itself.

> w.flat_map {|n| n+1 }



77
78
79
# File 'lib/wrapped/blank.rb', line 77

def flat_map
  self
end

#fmapObject Also known as: collect, map

Do nothing, returning itself.

> w.fmap {|n| n+1 }



84
85
86
# File 'lib/wrapped/blank.rb', line 84

def fmap
  self
end

#grepObject

Produces itself.



60
61
62
# File 'lib/wrapped/blank.rb', line 60

def grep(*)
  self
end

#presentObject

Does nothing, returning itself. This is chainable. See blank for its companion.

w.present.blank { puts “Missing” }



25
26
27
# File 'lib/wrapped/blank.rb', line 25

def present
  self
end

#present?Boolean

False; this is not an instance of a wrapped value.

Returns:

  • (Boolean)


65
66
67
# File 'lib/wrapped/blank.rb', line 65

def present?
  false
end

#rejectObject

Produces itself.



55
56
57
# File 'lib/wrapped/blank.rb', line 55

def reject
  self
end

#selectObject Also known as: find_all

Produces itself.



48
49
50
# File 'lib/wrapped/blank.rb', line 48

def select
  self
end

#unwrapObject

It is an error (specifically, an IndexError) to use this method.

Raises:

  • (IndexError)


6
7
8
# File 'lib/wrapped/blank.rb', line 6

def unwrap
  raise IndexError.new("Blank has no value")
end

#unwrap_or(default = nil) ⇒ Object

Produce the value that is passed in.

> w.unwrap_or(0)



13
14
15
16
17
18
19
# File 'lib/wrapped/blank.rb', line 13

def unwrap_or(default = nil)
  if block_given?
    yield
  else
    default
  end
end