Module: Unsound::Control
- Defined in:
- lib/unsound/control.rb
Class Method Summary collapse
-
.maybe(&block) ⇒ Data::Nothing, Data::Just
Execute the block.
-
.try(&block) ⇒ Data::Right, Data::Left
Try executing the block.
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
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.
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 |