Class: Monadic::Some

Inherits:
Object
  • Object
show all
Defined in:
lib/monadic/option.rb

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Some

Returns a new instance of Some.



18
19
20
# File 'lib/monadic/option.rb', line 18

def initialize(value)
  @value = value
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



48
49
50
# File 'lib/monadic/option.rb', line 48

def method_missing(m, *args)
  Option(@value.__send__(m, *args))
end

Instance Method Details

#==(other) ⇒ Object



62
63
64
65
# File 'lib/monadic/option.rb', line 62

def ==(other)
  return false unless other.is_a? Some
  @value == other.instance_variable_get(:@value)
end

#empty?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/monadic/option.rb', line 28

def empty?
  false
end

#fetch(default = None, &block) ⇒ Object Also known as: or, _



36
37
38
39
# File 'lib/monadic/option.rb', line 36

def fetch(default=None, &block)
  return block.call(@value)  if block_given?
  return @value
end

#map(func = nil, &block) ⇒ Object



43
44
45
46
# File 'lib/monadic/option.rb', line 43

def map(func = nil, &block)
  return Option(@value.map(&block)) if @value.is_a?(Enumerable)
  return Option((func || block).call(@value))
end

#select(func = nil, &block) ⇒ Object



52
53
54
55
56
# File 'lib/monadic/option.rb', line 52

def select(func = nil, &block)
  return Option(@value.select(&block)) if @value.is_a?(Enumerable)
  return None unless (func || block).call(@value)
  return self
end

#to_aryObject Also known as: to_a



22
23
24
25
# File 'lib/monadic/option.rb', line 22

def to_ary
  return [@value].flatten if @value.respond_to? :flatten
  return [@value]
end

#to_sObject



58
59
60
# File 'lib/monadic/option.rb', line 58

def to_s
  "Some(#{@value.to_s})"
end

#truly?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/monadic/option.rb', line 32

def truly?
  @value == true
end