Narnach-future 0.2.0 → 0.2.1
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.
- data/README.rdoc +7 -2
- data/future.gemspec +1 -1
- data/lib/future.rb +13 -6
- data/spec/future_spec.rb +12 -0
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -8,8 +8,8 @@ calls.
|
|
8
8
|
An example:
|
9
9
|
f = Future.new { sleep 5; 123}
|
10
10
|
puts f * 5 #=> 615
|
11
|
-
|
12
|
-
The call f.*
|
11
|
+
This future will take 5 seconds to run, then 123 is returned.
|
12
|
+
The call to f.* will block until the future has a return value.
|
13
13
|
|
14
14
|
Another example:
|
15
15
|
f1 = Future.new { sleep 5; 10}
|
@@ -20,6 +20,11 @@ Here it only takes 5 seconds to calculate the sum instead of the 9 it would take
|
|
20
20
|
|
21
21
|
== Recent changes
|
22
22
|
|
23
|
+
=== Version 0.2.1
|
24
|
+
* Cleared up the docs a little
|
25
|
+
* Futures can take arguments and pass them to their Thread
|
26
|
+
* Docfix: fixed typo
|
27
|
+
|
23
28
|
=== Version 0.2.0
|
24
29
|
* Replaced Future Module with Future class. The old syntax is no longer supported.
|
25
30
|
* Replaced one-time use of eval with undef_method.
|
data/future.gemspec
CHANGED
@@ -3,7 +3,7 @@ Gem::Specification.new do |s|
|
|
3
3
|
s.name = 'future'
|
4
4
|
s.summary = "A naieve implementation of futures"
|
5
5
|
s.description = "A naieve implementation of futures"
|
6
|
-
s.version = '0.2.
|
6
|
+
s.version = '0.2.1'
|
7
7
|
s.date = '2008-12-10'
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["Wes Oldenbeuving"]
|
data/lib/future.rb
CHANGED
@@ -5,23 +5,30 @@
|
|
5
5
|
#
|
6
6
|
# An example:
|
7
7
|
# f = Future.new { sleep 5; 123}
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# The call f.*
|
8
|
+
# f * 5 # => 615
|
9
|
+
# This future will take 5 seconds to run, then 123 is returned.
|
10
|
+
# The call to f.* will block until the future has a return value.
|
11
11
|
#
|
12
12
|
# Another example:
|
13
13
|
# f1 = Future.new { sleep 5; 10}
|
14
14
|
# f2 = Future.new { sleep 3; 20}
|
15
15
|
# f3 = Future.new { sleep 1; 30}
|
16
|
-
#
|
16
|
+
# f3 + f2 + f1 # => 60
|
17
17
|
# Here it only takes 5 seconds to calculate the sum instead of the 9 it would take when done sequentially.
|
18
|
+
#
|
19
|
+
# Futures have their own thread and thus don't have access to scope they were created from.
|
20
|
+
# If you do require values from outside the future, pass them to the Future when creating it:
|
21
|
+
#
|
22
|
+
# one = 1
|
23
|
+
# two = 2
|
24
|
+
# sum = Future.new(one, two) {|a,b| a + b} # => 3
|
18
25
|
class Future
|
19
26
|
(instance_methods - %w[__send__ __id__ object_id]).each do |meth|
|
20
27
|
undef_method meth
|
21
28
|
end
|
22
29
|
|
23
|
-
def initialize(&block)
|
24
|
-
@thread = Thread.new(&block)
|
30
|
+
def initialize(*args, &block)
|
31
|
+
@thread = Thread.new(*args, &block)
|
25
32
|
end
|
26
33
|
|
27
34
|
def method_missing(*args, &block)
|
data/spec/future_spec.rb
CHANGED
@@ -19,4 +19,16 @@ describe Future, '.new' do
|
|
19
19
|
diff.should < 0.3
|
20
20
|
diff.should > 0.1
|
21
21
|
end
|
22
|
+
|
23
|
+
it 'should have its own scope' do
|
24
|
+
now = Time.now
|
25
|
+
f1 = Future.new { now }
|
26
|
+
f1.should_not == now
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should be able to pass variables to scope of the future' do
|
30
|
+
now = Time.now
|
31
|
+
f1 = Future.new(now) { |time| time + 10 }
|
32
|
+
f1.should == now + 10
|
33
|
+
end
|
22
34
|
end
|