class Danom::Just
Useful for immediately failing when a nil pops up.
name = get_name(person) #=> nil # ... later somewhere name.upcase # Error!
Using a Just
catches the problem at the source
name = Just(get_name(person)) # Error!
Public Class Methods
new(value)
click to toggle source
@raise [Danom::Just::CannotBeNil] if value is provided as nil
Calls superclass method
# File lib/danom/just.rb, line 17 def initialize(value) raise CannotBeNil if !(Monad === value) && value == nil super end
Public Instance Methods
and_then(&block)
click to toggle source
Similar to Maybe#and_then
but raises an error if the value ever becomes nil.
@example
j = Just('hello') #=> Just text = ~j #=> 'hello'
@example Failing when becomes nil
j = Just({}) #=> Just j[:name] #=> Danom::Monad::CannotBeNil
@see Maybe#and_then
@see Monad#method_missing
@return [Just]
# File lib/danom/just.rb, line 44 def and_then(&block) Just.new block.call(@value) end
monad_value()
click to toggle source
@raise [Danom::Just::CannotBeNil]
Calls superclass method
# File lib/danom/just.rb, line 23 def monad_value val = super raise CannotBeNil if val == nil val end