try-catch 1.2.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 345103aaa3a3a13455a9efa6c7d8aaab523fdc3b
4
- data.tar.gz: d8862c5da29c4fba29311f2cbc7f3ec44df656ec
3
+ metadata.gz: 6a929e2dd08580f5df4943cc4e4c4ea2f9badaf1
4
+ data.tar.gz: c1d56de1a61958d47614b845fa8097a7778da03b
5
5
  SHA512:
6
- metadata.gz: a4ef0166e36f785139ff5efbc52c09fff386e0ca83ef83fb1e571381aa25f178cebf005305df66ac9d94ae150548b1933e9ffacf19879e0d0491af8dc4dd6114
7
- data.tar.gz: 22dee0fd8c9b6a6b7af555a052c5f7fa015c3d778e32eb8a5664066865f3137a0d4e44004b8e594e21452c98c698b791a9c2e514f71cdc24ce716997df9c293c
6
+ metadata.gz: 07dea44d045e46e182636eab8817c15514ae670b90801b921ad1acd81a75eb9a3825ad6ac0501ed4738b4fbe1b0523740def18fb182163b99dbc5b0e72dbfc65
7
+ data.tar.gz: ea8e64484f6cb46153da2b84f403d8c088056e3a35e6b98137ccb3f1966ecabb27e0ac064f08174c08818be6817c9af36da70e1dc123a41985aafa5d0aa26b11
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- try-catch (1.2.0)
4
+ try-catch (1.4.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/README.md CHANGED
@@ -11,19 +11,21 @@ if you new to ruby and missing the try catch block here is a simple one for you
11
11
 
12
12
  ```ruby
13
13
 
14
- require 'try-catch'
14
+ require 'try-catch'
15
15
 
16
- try { hello world }.catch{ "not hello world " }
17
- #> "not hello world"
16
+ try { hello world }.catch{ "not hello world " }
17
+ #> "not hello world"
18
18
 
19
- try { "hello world".asdaf }.catch( NoMethodError ) { |ex| "there is and error, because #{ex}" }
20
- #> there is and error, because undefined method `asdaf' for "hello world":String
19
+ try { "hello world".asdaf }.catch( NoMethodError ) { |ex| "there is and error, because #{ex}" }
20
+ #> there is and error, because undefined method `asdaf' for "hello world":String
21
21
 
22
- #> you can cain up multiple catch for specific error
23
- try { "hello world".asdaf }.catch( NoMethodError ) { |ex| "there is and error, because #{ex}" }
22
+ #> you can cain up multiple catch for specific error
23
+ try { "hello world".asdaf }.catch( NoMethodError ) { |ex| "there is and error, because #{ex}" }
24
24
 
25
- puts try { "hello world".asdaf }.catch(ArgumentError) { "it was and argument error" }.catch( NoMethodError ) { |ex| "bla bla #{ex}" }
26
- #> "bla bla undefined method `asdaf' for "hello world":String"
25
+ puts try { "hello world".asdaf }.catch(ArgumentError) { "it was and argument error" }.catch( NoMethodError ) { |ex| "bla bla #{ex}" }
26
+ #> "bla bla undefined method `asdaf' for "hello world":String"
27
+
28
+ puts try_nc { "hello world".asd } #> non cached try block
27
29
 
28
30
  ```
29
31
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.0
1
+ 1.4.0
@@ -0,0 +1,8 @@
1
+ require_relative '../lib/try-catch/memory'
2
+ require_relative '../lib/try-catch/functions'
3
+ require_relative '../lib/try-catch/syntax'
4
+
5
+ require 'debugger'
6
+
7
+ try { puts("hello world".z) }
8
+ catch(NoMethodError) { |ex| puts ex.to_s.upcase }
@@ -0,0 +1,55 @@
1
+ require_relative '../lib/try-catch/memory'
2
+ require_relative '../lib/try-catch/functions'
3
+ require_relative '../lib/try-catch/syntax'
4
+
5
+ module TestA
6
+ class << self
7
+
8
+ def hello
9
+ "hello world"
10
+ end
11
+
12
+ def complex
13
+ "complex"
14
+ end
15
+
16
+ end
17
+ end
18
+
19
+ module TestB
20
+ class << self
21
+
22
+ def hello
23
+ "hello world"
24
+ end
25
+
26
+ def complex hello
27
+ "complex: #{hello.to_s}"
28
+ end
29
+
30
+ end
31
+ end
32
+
33
+
34
+ module TestC
35
+ class << self
36
+
37
+ def method_missing method_name, *args
38
+ return try?{ ::TestA.__send__(method_name,*args) } || try?{ ::TestB.__send__(method_name,*args) }
39
+ end
40
+
41
+
42
+ end
43
+ end
44
+
45
+ puts TestC.hello
46
+ puts TestC.complex
47
+ puts TestC.complex "sup!"
48
+
49
+ =begin
50
+
51
+ hello world
52
+ complex
53
+ complex: sup!
54
+
55
+ =begin
@@ -25,6 +25,44 @@ module TryCatch
25
25
 
26
26
  end
27
27
 
28
+ # no cache version !
29
+ def try_no_cache( *exception_classes, &block )
30
+
31
+ exception_classes.each do |exception_class|
32
+ unless exception_class <= Exception
33
+ raise ArgumentError, "invalid Exception: #{exception_class}"
34
+ end
35
+ end
36
+
37
+ exception_classes.push(Exception) if exception_classes.empty?
38
+
39
+ begin
40
+ return block.call
41
+ rescue *exception_classes => ex
42
+ return ex
43
+ end
44
+
45
+ end
46
+
47
+ # try that return nil overall exception captured block fail to run
48
+ def try?( *exception_classes, &block )
49
+
50
+ exception_classes.each do |exception_class|
51
+ unless exception_class <= Exception
52
+ raise ArgumentError, "invalid Exception: #{exception_class}"
53
+ end
54
+ end
55
+
56
+ exception_classes.push(Exception) if exception_classes.empty?
57
+
58
+ begin
59
+ return block.call
60
+ rescue *exception_classes
61
+ return nil
62
+ end
63
+
64
+ end
65
+
28
66
  def catch( self_object, *exception_classes, &block )
29
67
 
30
68
  exception_classes.each do |exception_class|
@@ -4,7 +4,17 @@ module TryCatch
4
4
  module EXT::Object
5
5
 
6
6
  def try *exception_classes, &block
7
- ::TryCatch::Function.try self, *exception_classes, &block
7
+ ::TryCatch::Function.try( self, *exception_classes, &block )
8
+ end
9
+
10
+ def try_nc *exception_classes, &block
11
+ ::TryCatch::Function.try_no_cache( *exception_classes, &block )
12
+ end
13
+
14
+ alias :try_no_cache :try_nc
15
+
16
+ def try? *exception_classes, &block
17
+ ::TryCatch::Function.try?( *exception_classes, &block )
8
18
  end
9
19
 
10
20
  def catch *exception_classes, &block
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: try-catch
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-22 00:00:00.000000000 Z
11
+ date: 2014-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -65,6 +65,8 @@ files:
65
65
  - README.md
66
66
  - Rakefile
67
67
  - VERSION
68
+ - examples/try_catch.rb
69
+ - examples/try_return_nil.rb
68
70
  - files.rb
69
71
  - lib/try-catch.rb
70
72
  - lib/try-catch/functions.rb