Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

Move Result block behavior to Result::Generic#try

+20 -14
+2 -14
lib/literal.rb
··· 82 82 Literal::Brand.new(...) 83 83 end 84 84 85 - def self.Result(success_type, failure_type) 85 + def self.Result(success_type, failure_type, &) 86 86 result_type = Result::Generic.new(success_type, failure_type) 87 87 88 88 if block_given? 89 - caught = catch do |ball| 90 - emitter = Result::Emitter.new(type: result_type, ball:) 91 - yield(emitter) 92 - end 93 - 94 - case caught 95 - when Result::Thrown 96 - Literal.check(caught.result, result_type) 97 - caught.result 98 - else 99 - Literal.check(caught, success_type) 100 - result_type.success(caught) 101 - end 89 + result_type.try(&) 102 90 else 103 91 result_type 104 92 end
+18
lib/literal/result/generic.rb
··· 21 21 end 22 22 end 23 23 24 + def try 25 + raise ArgumentError unless block_given? 26 + 27 + caught = catch do |ball| 28 + emitter = Literal::Result::Emitter.new(type: self, ball:) 29 + yield(emitter) 30 + end 31 + 32 + case caught 33 + when Literal::Result::Thrown 34 + Literal.check(caught.result, self) 35 + caught.result 36 + else 37 + Literal.check(caught, @success_type) 38 + success(caught) 39 + end 40 + end 41 + 24 42 def success(value) 25 43 Literal::Success.new( 26 44 value,