This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> f= Fiber.new { puts "x" } | |
> f.resume | |
x | |
=> nil | |
> f.resume | |
FiberError: dead fiber called |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> f = Fiber.new{ puts "first"; Fiber.yield; puts "second" } | |
> f.resume | |
first | |
=> nil | |
> f.resume | |
second | |
=> nil | |
> f.resume | |
FiberError: dead fiber called |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> f = Fiber.new{ Fiber.yield 2; Fiber.yield 3; Fiber.yield 5; Fiber.yield 7; } | |
> f.resume | |
=> 2 | |
> f.resume | |
=> 3 | |
> f.resume | |
=> 5 | |
> f.resume | |
=> 7 | |
> f.resume |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f = Fiber.new{ Fiber.yield 7; "OMG Fibers"} | |
> f.resume | |
=> 7 | |
> f.resume | |
=> "OMG Fibers" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> f = Fiber.new{ 5.times{|x| Fiber.yield(x+10) }; nil } | |
> 6.times{ puts f.resume.inspect } | |
10 | |
11 | |
12 | |
13 | |
14 | |
nil |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> f = Fiber.new{ loop{ Fiber.yield rand() } } | |
> f.resume | |
=> 0.284433348607815 | |
> f.resume | |
=> 0.49985331837809865 | |
> f.resume | |
=> 0.41816746846163666 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> f1 = Fiber.new{ loop{ Fiber.yield rand() } } | |
> f2 = Fiber.new{ ary = [true, false]; loop{ Fiber.yield(ary.sample) } } | |
> f1.resume | |
=> 0.39994797202857346 | |
> f2.resume | |
=> false | |
> f2.resume | |
=> false | |
> f1.resume | |
=> 0.9078216956199455 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby-1.9.2-p180-fastrequire :057 > f1 = Fiber.new{ i = 1; loop{ i+= 1; Fiber.yield(i) if Prime.prime?(i) } } | |
=> #<Fiber:0x0000000234a150> | |
ruby-1.9.2-p180-fastrequire :058 > f2 = Fiber.new{ i = 1; loop{ i+= 1; Fiber.yield(i**2) } } | |
=> #<Fiber:0x00000002329c48> | |
ruby-1.9.2-p180-fastrequire :060 > 4.times do | |
ruby-1.9.2-p180-fastrequire :061 > puts f1.resume + f2.resume | |
ruby-1.9.2-p180-fastrequire :062?> end | |
6 | |
12 | |
21 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> require 'prime' | |
> f1 = Fiber.new{ i = 1; loop{ i+= 1; Fiber.yield(i) if Prime.prime?(i) } } | |
> f2 = Fiber.new{ i = 1; loop{ i+= 1; Fiber.yield(i**2) } } | |
> 4.times do | |
> puts f1.resume + f2.resume | |
> end | |
6 | |
12 | |
21 | |
32 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
results = [] | |
f1 = Fiber.new do | |
i = 1 | |
loop do | |
i+= 1 | |
if Prime.prime?(i) | |
results << i | |
Fiber.yield(i) | |
end | |
end |