Class: Just

Inherits:
Maybe show all
Defined in:
lib/ruby-maybe.rb

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Just

Returns a new instance of Just.



14
15
16
# File 'lib/ruby-maybe.rb', line 14

def initialize(value)
  @value = value
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/ruby-maybe.rb', line 5

def method_missing(method_name, *args, &block)
  values = args.map { |arg|
    return Nothing.new if arg == Nothing.new
    arg.kind_of?(Just) ? arg.value : arg
  }

  Just.new(@value.public_send(method_name, *values, &block))
end

Instance Method Details

#==(object) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/ruby-maybe.rb', line 24

def ==(object)
  if object.class == Just
    object.value == self.value
  else
    false
  end
end

#_extract!Object



32
33
34
# File 'lib/ruby-maybe.rb', line 32

def _extract!
  value
end

#bind(&block) ⇒ Object



18
19
20
21
22
# File 'lib/ruby-maybe.rb', line 18

def bind(&block)
  computed = block.call(@value)
  warn("Not returning a Maybe from #bind is really bad form...") unless computed.kind_of?(Maybe)
  computed
end