Module: Unsound::Control

Defined in:
lib/unsound/control.rb

Class Method Summary collapse

Class Method Details

.maybe(&block) ⇒ Data::Nothing, Data::Just

Execute the block. If the block results in Nil, return Data::Nothing, otherwise wrap the result in a Data::Just

Parameters:

  • block (Block)

    the block to execute

Returns:



26
27
28
29
30
31
32
# File 'lib/unsound/control.rb', line 26

def maybe(&block)
  if (result = block.call).nil?
    Data::Nothing.new
  else
    Data::Just.new(result)
  end
end

.try(&block) ⇒ Data::Right, Data::Left

Try executing the block. If the block raises an exception wrap it in a Data::Left, otherwise wrap it in a Data::Right.

Parameters:

  • block (Block)

    the block to execute

Returns:



14
15
16
17
18
# File 'lib/unsound/control.rb', line 14

def try(&block)
  Data::Right.new(block.call)
rescue
  Data::Left.new($ERROR_INFO)
end