aw 0.1.13 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.md +1 -1
  3. data/README.md +52 -9
  4. data/lib/aw/fork.rb +4 -3
  5. data/lib/aw.rb +27 -2
  6. metadata +7 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9af984fb0d47a9566b6ca5c9ac4a7208328cb444b504865e4fff631bb4122db7
4
- data.tar.gz: 8c9759ef06861c6518cedd5867d85ebb04b62a01512a42abd94b401a3d67aa92
3
+ metadata.gz: 72301167c4e8ea2ca3f8f2ab8bd7295822577c6539aeedf7bbc67b0feeb2eaf5
4
+ data.tar.gz: 0e4afe4ec73d3b19a3bdd2e644f2a45366ad9ac4ad73c7c57c7cd290face4639
5
5
  SHA512:
6
- metadata.gz: b950326ea7dc9a6846f079e75325e14d90d0261466dbd896dc57644eba8ef08ef8935364e756b6e41ed9f8b11cef68bb9752c9d3ad146daaf375f0b5334920d3
7
- data.tar.gz: f21141ba62144f1b86ebead8c2816625c243272c51e671fcbdf95a28b8f81c8716dae4e7063427722094db3e661e8ceca2fdf757f2bf26612f9673dc35754b26
6
+ metadata.gz: 72244a26c4a020ef1a5b185ca12d1d06dd87c3814d7acc55a3147f9df06eb414de9dbf4968d3e3d613d022d1475e48f7b60d52074e18047023e2fc6f02a0faa1
7
+ data.tar.gz: b6176b99c583a856bff7b98db9cbb9007b4ece7111b58739bfabab5c88324cde06581a46db4b9c8151ae7eb213abf05cc0210c57b7f04dcaf6beabcf299cec2c
data/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2015-2021 Cyril Kato
3
+ Copyright (c) 2015-2022 Cyril Kato
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -6,9 +6,11 @@
6
6
  [![RuboCop](https://github.com/fixrb/aw/workflows/RuboCop/badge.svg?branch=main)](https://github.com/fixrb/aw/actions?query=workflow%3Arubocop+branch%3Amain)
7
7
  [![License](https://img.shields.io/github/license/fixrb/aw?label=License&logo=github)](https://github.com/fixrb/aw/raw/main/LICENSE.md)
8
8
 
9
- > Aw, fork! 😬
9
+ > Aw, fork 😬
10
10
 
11
- Creates a sub-process to execute a block inside, and return the result.
11
+ ![Aw](https://github.com/fixrb/aw/raw/main/img/aw.jpg)
12
+
13
+ Creates a sub-process to execute a block inside, and returns what it returns (or a boolean).
12
14
 
13
15
  ## Installation
14
16
 
@@ -21,7 +23,7 @@ gem "aw"
21
23
  And then execute:
22
24
 
23
25
  ```sh
24
- bundle
26
+ bundle install
25
27
  ```
26
28
 
27
29
  Or install it yourself as:
@@ -32,26 +34,67 @@ gem install aw
32
34
 
33
35
  ## Usage
34
36
 
35
- Execute a block of code in a sub-process, and return the result to the current process:
37
+ To make __Aw__ available:
38
+
39
+ ```ruby
40
+ require "aw"
41
+ ```
42
+
43
+ There are two methods:
44
+
45
+ - `fork!`
46
+ - `fork?`
47
+
48
+ ### Method `.fork!`
49
+
50
+ Executes a block of code in a sub-process, and returns the result:
36
51
 
37
52
  ```ruby
38
53
  Aw.fork! { 6 * 7 } # => 42
39
54
  ```
40
55
 
41
- Therefore, when the execution of a block of code causes side effects, they are limited to the sub-process:
56
+ When the execution of a block of code causes side effects, these are limited to the sub-process:
42
57
 
43
58
  ```ruby
44
- arr = ["foo"]
59
+ arr = ["foo"] # => ["foo"]
45
60
 
46
61
  Aw.fork! { arr << "FUU" } # => ["foo", "FUU"]
47
62
 
48
63
  arr # => ["foo"]
49
64
  ```
50
65
 
51
- Exceptions raised within a block of code are propagated:
66
+ Exceptions raised in a block of code are propagated:
67
+
68
+ ```ruby
69
+ Aw.fork! { nil + 1 }
70
+ ```
71
+
72
+ results in the error:
73
+
74
+ > `NoMethodError` (undefined method `+' for nil:NilClass)
75
+
76
+ ### Method `.fork?`
77
+
78
+ Executes a block of code in a sub-process, and returns `true` if no exception is thrown:
79
+
80
+ ```ruby
81
+ Aw.fork? { 6 * 7 } # => true
82
+ ```
83
+
84
+ When the execution of a block of code causes side effects, these are limited to the sub-process:
85
+
86
+ ```ruby
87
+ arr = ["foo"] # => ["foo"]
88
+
89
+ Aw.fork? { arr << "FUU" } # => true
90
+
91
+ arr # => ["foo"]
92
+ ```
93
+
94
+ When an exception is raised in a code block, `false` is returned:
52
95
 
53
96
  ```ruby
54
- Aw.fork! { nil + 1 } # => NoMethodError (undefined method `+' for nil:NilClass)
97
+ Aw.fork? { nil + 1 } # => false
55
98
  ```
56
99
 
57
100
  ## Contact
@@ -64,7 +107,7 @@ __Aw__ follows [Semantic Versioning 2.0](https://semver.org/).
64
107
 
65
108
  ## License
66
109
 
67
- The [gem](https://rubygems.org/gems/aw) is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
110
+ The [gem](https://rubygems.org/gems/aw) is available as open source under the terms of the [MIT License](https://github.com/fixrb/aw/raw/main/LICENSE.md).
68
111
 
69
112
  ***
70
113
 
data/lib/aw/fork.rb CHANGED
@@ -34,7 +34,8 @@ module Aw
34
34
  # @example Computes `6 * 7` in a sub-process and returns `42` to the current process.
35
35
  # call { 6 * 7 } # => 42
36
36
  #
37
- # @return [#object_id] The computed value.
37
+ # @raise [Exception] Exceptions raised in a block of code are propagated.
38
+ # @return [#object_id] Returns the value that has been returned in the block.
38
39
  def call(&block)
39
40
  pid = fork_and_return_pid(&block)
40
41
  write.close
@@ -53,7 +54,7 @@ module Aw
53
54
  #
54
55
  # @return [Integer] The ID of the created sub-process.
55
56
  def fork_and_return_pid
56
- fork do
57
+ ::Process.fork do
57
58
  # :nocov:
58
59
 
59
60
  read.close
@@ -67,7 +68,7 @@ module Aw
67
68
  # rubocop:enable Lint/RescueException
68
69
 
69
70
  ::Marshal.dump(result, write)
70
- exit!(true)
71
+ ::Process.exit!(true)
71
72
 
72
73
  # :nocov:
73
74
  end
data/lib/aw.rb CHANGED
@@ -5,6 +5,9 @@
5
5
  # @example Computes `6 * 7` in a sub-process and returns `42` to the current process.
6
6
  # Aw.fork! { 6 * 7 } # => 42
7
7
  #
8
+ # @example Computes `6 * 7` in a sub-process and returns `true` to the current process if no exception is thrown.
9
+ # Aw.fork? { 6 * 7 } # => true
10
+ #
8
11
  # @api public
9
12
  module Aw
10
13
  # Runs the block inside a sub-process, and returns the computed value.
@@ -12,13 +15,35 @@ module Aw
12
15
  # @param block [Proc] The code to run in a sub-process.
13
16
  #
14
17
  # @example Computes `6 * 7` in a sub-process and returns `42` to the current process.
15
- # fork! { 6 * 7 } # => 42
18
+ # Aw.fork! { 6 * 7 } # => 42
19
+ #
20
+ # @example Computes `nil + 1` in a sub-process and raises `NoMethodError` to the current process.
21
+ # Aw.fork! { nil + 1 } # => raise NoMethodError (undefined method `+' for nil:NilClass)
16
22
  #
17
- # @return [#object_id] The computed value.
23
+ # @raise [Exception] Exceptions raised in a block of code are propagated.
24
+ # @return [#object_id] Returns the value that has been returned in the block.
18
25
  def self.fork!(&block)
19
26
  read, write = ::IO.pipe
20
27
  Fork.new(read, write).call(&block)
21
28
  end
29
+
30
+ # Runs the block inside a sub-process, and returns `true` if no exception is
31
+ # thrown. Otherwise when an exception is raised, `false` is returned.
32
+ #
33
+ # @param block [Proc] The code to run in a sub-process.
34
+ #
35
+ # @example Computes `6 * 7` in a sub-process and returns `true` to the current process.
36
+ # Aw.fork? { 6 * 7 } # => true
37
+ #
38
+ # @example Computes `nil + 1` in a sub-process and returns `false` to the current process.
39
+ # Aw.fork? { nil + 1 } # => false
40
+ #
41
+ # @return [Boolean] Returns `true` if stat is successful, `false` if not.
42
+ def self.fork?(&block)
43
+ pid = ::Process.fork(&block)
44
+ _, status = ::Process.wait2(pid)
45
+ status.success?
46
+ end
22
47
  end
23
48
 
24
49
  require_relative File.join("aw", "fork")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aw
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cyril Kato
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-31 00:00:00.000000000 Z
11
+ date: 2022-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,7 +122,8 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
- description: Creates a sub-process to execute a block inside, and returns the result.
125
+ description: Creates a sub-process to execute a block inside, and returns what it
126
+ returns (or a boolean).
126
127
  email: contact@cyril.email
127
128
  executables: []
128
129
  extensions: []
@@ -135,7 +136,8 @@ files:
135
136
  homepage: https://github.com/fixrb/aw
136
137
  licenses:
137
138
  - MIT
138
- metadata: {}
139
+ metadata:
140
+ rubygems_mfa_required: 'true'
139
141
  post_install_message:
140
142
  rdoc_options: []
141
143
  require_paths:
@@ -154,5 +156,5 @@ requirements: []
154
156
  rubygems_version: 3.1.6
155
157
  signing_key:
156
158
  specification_version: 4
157
- summary: "Aw, fork! \U0001F62C"
159
+ summary: "Aw, fork \U0001F62C"
158
160
  test_files: []