try 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/try.rb +43 -0
  3. metadata +51 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3c5876e6f7db8345c101789e5634e1cb2d149c98
4
+ data.tar.gz: fd06368b745e5d22e1c444f34e507b33ee45077e
5
+ SHA512:
6
+ metadata.gz: c45036d9a15d19225dd4052d09c2cc9c3fec978a3d73f6b5692ff618c18235f27494621cb8f677c0b7a4ac6da08f4749be704aa4fd7f98db6bdf151de56c5fd3
7
+ data.tar.gz: d08624af2df6a00a35d75b62145bb4b680133997f208883e6871f92e78a543fed2b5c449c6ab4a2f090239f6347d8b76fdfedd5e1f6863d7200e7de4698cb3a0
data/lib/try.rb ADDED
@@ -0,0 +1,43 @@
1
+
2
+ module Try
3
+ #
4
+ # Evaluates the given block and returns its value.
5
+ # If an exception is raised, that exception is returned instead.
6
+ #
7
+ def try &bk
8
+ yield
9
+ rescue Exception => ex
10
+ ex
11
+ end
12
+
13
+ #
14
+ # Evaluates the given block and returns its value.
15
+ #
16
+ # Specific exception types can be trapped by passing them as
17
+ # parameters.
18
+ #
19
+ # Additionally, specific exception types can default to a fallback
20
+ # value if passed as (({Type => value})) pairs.
21
+ #
22
+ # Any un-trapped exception is raised normally.
23
+ #
24
+ # @param errs a list of exception types to trap.
25
+ #
26
+ def trap *errs, &bk
27
+ hash = {}
28
+ hash = errs.pop if errs.last.is_a? Hash
29
+ errs.each {|ec| hash[ec] = ec }
30
+ yield
31
+ rescue Exception => ex
32
+ errs.each do |klass|
33
+ return ex if klass.instance_of?(Module) ? ex.kind_of?(klass) : ex.is_a?(klass)
34
+ end
35
+ hash.each_pair do |klass,value|
36
+ return value if klass.instance_of?(Module) ? ex.kind_of?(klass) : ex.is_a?(klass)
37
+ end
38
+ raise
39
+ end
40
+
41
+ extend self
42
+ end
43
+
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: try
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Kerwin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Lazy mechanisms to capture exceptions on-the-fly.
14
+ email: matthew@kerwin.net.au
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/try.rb
20
+ homepage: http://rubygems.org/gems/try
21
+ licenses:
22
+ - Simplified BSD License
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options:
26
+ - --title
27
+ - 'Try: do, or do not'
28
+ - --main
29
+ - Try
30
+ - --line-numbers
31
+ - --tab-width
32
+ - '2'
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.0.0
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: 'Try: do, or do not'
51
+ test_files: []