Class: Monadify::Some

Inherits:
Option
  • Object
show all
Defined in:
lib/monadify/option/some.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Option

from

Constructor Details

#initialize(val) ⇒ Some

Returns a new instance of Some.



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

def initialize(val)
  @value = val
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  false
end

#flatmapObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/monadify/option/some.rb', line 29

def flatmap
  begin
    return_option = yield value
    case return_option
    when Monadify::None
      return_option
    when Monadify::Some
      Monadify::Some.new(return_option.value)
    else
      raise NotAnOptionError
    end
  rescue NotAnOptionError
    raise ArgumentError, 'The block should return an Option'
  rescue ArgumentError, NameError, TypeError
    raise
  rescue
    Monadify::None.new
  end
end

#mapObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/monadify/option/some.rb', line 14

def map
  begin
    return_val = yield value
    if return_val.nil?
      Monadify::None.new
    else
      Monadify::Some.new(return_val)
    end
  rescue ArgumentError, NameError, TypeError
    raise
  rescue
    Monadify::None.new
  end
end