Class: Try
- Inherits:
-
Object
- Object
- Try
- Defined in:
- lib/Olib/try/try.rb
Instance Attribute Summary collapse
-
#result ⇒ Object
Returns the value of attribute result.
-
#task ⇒ Object
Returns the value of attribute task.
Class Method Summary collapse
Instance Method Summary collapse
- #failed? ⇒ Boolean
-
#initialize(&task) ⇒ Try
constructor
A new instance of Try.
- #match?(exp) ⇒ Boolean
- #recover ⇒ Object
- #success? ⇒ Boolean
- #then ⇒ Object
Constructor Details
#initialize(&task) ⇒ Try
Returns a new instance of Try.
10 11 12 13 |
# File 'lib/Olib/try/try.rb', line 10 def initialize(&task) @task = task run! end |
Instance Attribute Details
#result ⇒ Object
Returns the value of attribute result.
2 3 4 |
# File 'lib/Olib/try/try.rb', line 2 def result @result end |
#task ⇒ Object
Returns the value of attribute task.
2 3 4 |
# File 'lib/Olib/try/try.rb', line 2 def task @task end |
Class Method Details
Instance Method Details
#failed? ⇒ Boolean
29 30 31 |
# File 'lib/Olib/try/try.rb', line 29 def failed? @result.class.ancestors.include? Exception end |
#match?(exp) ⇒ Boolean
45 46 47 48 49 50 51 52 53 |
# File 'lib/Olib/try/try.rb', line 45 def match?(exp) if failed? @result..match(exp) elsif @result.respond_to?(:match) @result.match(exp) else raise Exception.new "cannot match class #{@result.class}" end end |
#recover ⇒ Object
37 38 39 |
# File 'lib/Olib/try/try.rb', line 37 def recover Try.new { yield @result } if failed? end |
#success? ⇒ Boolean
33 34 35 |
# File 'lib/Olib/try/try.rb', line 33 def success? !failed? end |