Class: Monad::Maybe::Just

Inherits:
Base show all
Defined in:
lib/monad/maybe/json.rb,
lib/monad/maybe/just.rb

Instance Attribute Summary

Attributes inherited from Base

#value

Instance Method Summary collapse

Methods inherited from Base

#<<, #and, #maybe?, #then, #to_list, #to_maybe

Constructor Details

#initialize(value) ⇒ Just



4
5
6
# File 'lib/monad/maybe/just.rb', line 4

def initialize(value)
  @value = value
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



8
9
10
# File 'lib/monad/maybe/just.rb', line 8

def method_missing(method, *args)
  value.send(method, *args).to_maybe
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  self === other || self.value == other
end

#===(other) ⇒ Object



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

def ===(other)
  other.just? && self.value == other.value
end

#bind(fn) ⇒ Object



32
33
34
# File 'lib/monad/maybe/just.rb', line 32

def bind(fn)
  fn[@value].to_maybe
end

#equal?(other) ⇒ Boolean



48
49
50
# File 'lib/monad/maybe/just.rb', line 48

def equal?(other)
  other.__id__ == self.__id__
end

#inspectObject



52
53
54
# File 'lib/monad/maybe/just.rb', line 52

def inspect
  "just(#{value.inspect})"
end

#just?Boolean



24
25
26
# File 'lib/monad/maybe/just.rb', line 24

def just?
  true
end

#maybe(&blk) ⇒ Object



28
29
30
# File 'lib/monad/maybe/just.rb', line 28

def maybe(&blk)
  bind(blk)
end

#nil?Boolean



36
37
38
# File 'lib/monad/maybe/just.rb', line 36

def nil?
  false
end

#nothing?Boolean



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

def nothing?
  false
end

#something?Boolean



20
21
22
# File 'lib/monad/maybe/just.rb', line 20

def something?
  true
end

#to_aObject



61
62
63
# File 'lib/monad/maybe/just.rb', line 61

def to_a
  [self]
end

#to_json(*args) ⇒ Object



9
10
11
# File 'lib/monad/maybe/json.rb', line 9

def to_json(*args)
  value.to_json(*args)
end

#to_sObject Also known as: to_str



56
57
58
# File 'lib/monad/maybe/just.rb', line 56

def to_s
  value.to_s
end

#unwrap(val) ⇒ Object



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

def unwrap(val)
  value
end