num_threads 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jesse Storimer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,58 @@
1
+ # num_threads
2
+
3
+ A simple short-hand for running a block of Ruby code in multiple concurrent threads.
4
+
5
+ ``` ruby
6
+ array = Array.new(10)
7
+
8
+ 10.threads do
9
+ array.pop
10
+ # interesting stuff in here
11
+ end
12
+ ```
13
+
14
+ ## Huh?
15
+
16
+ Call the `threads` method on a `Fixnum` and pass it a block. It spawns that many threads, runs the block, then calls `Thread#join` on each thread to wait for it to finish.
17
+
18
+ ## How is this useful?
19
+
20
+ I was writing a lot of simple example code that spawned a few threads for [Working With Ruby Threads](http://workingwithrubythreads.com/). I got sick of writing this code:
21
+
22
+ ``` ruby
23
+ threads = []
24
+
25
+ 10.times do
26
+ threads << Thread.new do
27
+ # interesting stuff in here
28
+ end
29
+ end
30
+
31
+ threads.each(&:join)
32
+ ```
33
+
34
+ The `interesting stuff` is hidden in too many layers of boilerplate. I wanted to write this:
35
+
36
+ ``` ruby
37
+ 10.threads do
38
+ # interesting stuff in here
39
+ end
40
+ ```
41
+
42
+ These two code examples are equivalent. That's the whole point of the gem. Now the interesting stuff is easier to see with less boilerplate surrounding it.
43
+
44
+ I use this for writing example code. I imagine could also be useful in writing one-off scripts.
45
+
46
+ ## Installation
47
+
48
+ Install with:
49
+
50
+ $ gem install num_threads
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create new Pull Request
@@ -0,0 +1,18 @@
1
+ module NumThreads
2
+ def threads(&blk)
3
+ threads = []
4
+
5
+ self.times do
6
+ threads << Thread.new do
7
+ blk.call
8
+ end
9
+ end
10
+
11
+ threads.each(&:join)
12
+ end
13
+ end
14
+
15
+ class Fixnum
16
+ include NumThreads
17
+ end
18
+
@@ -0,0 +1,4 @@
1
+ module NumThreads
2
+ VERSION = '0.0.1'
3
+ end
4
+
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: num_threads
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Jesse Storimer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-26 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple short-hand for running a block of code across multiple threads.
15
+ email:
16
+ - jesse@jstorimer.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/num_threads.rb
22
+ - lib/num_threads/version.rb
23
+ - README.md
24
+ - LICENSE.txt
25
+ homepage: http://github.com/jstorimer/num_threads
26
+ licenses: []
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: !binary |-
36
+ MA==
37
+ none: false
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: !binary |-
43
+ MA==
44
+ none: false
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.24
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: A simple short-hand for running a block of code across multiple threads.
51
+ test_files: []