Class: Some

Inherits:
Object
  • Object
show all
Includes:
Option
Defined in:
lib/optional/some.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Option

#match

Methods included from Option::Enumerable

#detect, #flat_map, #flatten, #grep, #juxt, #map, #map_through, #reduce, #reject, #select, #to_ary

Constructor Details

#initialize(value) ⇒ Some

Returns a new instance of Some.



6
7
8
# File 'lib/optional/some.rb', line 6

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'lib/optional/some.rb', line 4

def value
  @value
end

Class Method Details

.[](*values) ⇒ Object



49
50
51
# File 'lib/optional/some.rb', line 49

def self.[](*values)
  new(values.size == 1 ? values.first : values)
end

Instance Method Details

#&(other) ⇒ Object



26
27
28
# File 'lib/optional/some.rb', line 26

def & other
  other.and_option(self)
end

#==(other) ⇒ Object



30
31
32
# File 'lib/optional/some.rb', line 30

def == other
  other.is_a?(Some) && value == other.value
end

#each(&block) ⇒ Object



10
11
12
# File 'lib/optional/some.rb', line 10

def each &block
  block.call(value)
end

#merge(other, &block) ⇒ Object



38
39
40
41
42
43
# File 'lib/optional/some.rb', line 38

def merge(other, &block)
  other.match do |m|
    m.some { |v| block.nil? ? Some[*value, v] : Some[block.call(value, v)] }
    m.none { self }
  end
end

#none?(&block) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/optional/some.rb', line 14

def none?(&block)
  block.nil? ? false : super
end

#some?(type = value.class) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/optional/some.rb', line 22

def some?(type=value.class)
  value.class == type
end

#to_sObject



45
46
47
# File 'lib/optional/some.rb', line 45

def to_s
  "Some[#{value.inspect}]"
end

#value_or(default = nil) ⇒ Object



18
19
20
# File 'lib/optional/some.rb', line 18

def value_or(default=nil)
  value
end

#|(other) ⇒ Object



34
35
36
# File 'lib/optional/some.rb', line 34

def | other
  self
end