Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fibers

 Fibers

Denis Defreyne

March 02, 2017
Tweet

More Decks by Denis Defreyne

Other Decks in Technology

Transcript

  1. 7 f = Fiber.new do puts "hi" return puts "bye"

    end f.resume hi sample.rb:3:
 in`block in <main>':
 unexpected return
 (LocalJumpError)
  2. 11 f = Fiber.new do puts "hi" end f.resume f.resume

    hi a.rb:6:
 in `resume':
 dead fiber called
 (FiberError)
  3. 15 Create a fiber Fiber.new { … } Start running

    a fiber f.resume Resume an running fiber f.resume Return from a fiber Fiber.yield
  4. 19 f = Fiber.new do |a| puts a Fiber.yield puts

    'bye' end f.resume('hi') f.resume hi
 bye
  5. 20 f = Fiber.new do |a| puts a b =

    Fiber.yield puts b end f.resume('hi') f.resume('bye')
  6. 20 f = Fiber.new do |a| puts a b =

    Fiber.yield puts b end f.resume('hi') f.resume('bye') hi bye
  7. 24 f = Fiber.new do puts 'hi' Fiber.yield 'bye' end

    f.resume puts f.resume hi
 bye
  8. 26 Create a fiber Fiber.new { … } Start running

    a fiber f.resume(…) Resume an running fiber f.resume(…) Return from a fiber Fiber.yield(…)
  9. 31 
 f = Fiber.new do Fiber.yield(0) Fiber.yield(1) Fiber.yield(2) …

    end puts f.resume puts f.resume puts f.resume 0 1 2
  10. 32 f = Fiber.new do v = 0 loop do

    Fiber.yield(v) v += 1 end end puts f.resume puts f.resume puts f.resume 0 1 2
  11. 33 class Ints def initialize @f = Fiber.new { …

    } end def each loop do yield(@f.resume) end end end
  12. 34 Ints.new.each do |i| puts i end 0 1 2

    3 4 5 6 7 8 9 10 11 12 13
  13. 38 What is your first name? > Denis Hi, Denis.

    What is your last name? > Defreyne Got it — Defreyne! Thanks for your help, Denis Defreyne! Bye-bye for now!
  14. 39 first_name = ask "What is your first name?" say

    "Hi, #{first_name}." last_name = ask "What is your last name?" say "Got it -- #{last_name}!" say "Thanks for your help, #{first_name}!" say "Bye-bye for now!" stop
  15. 40 def ask(s) puts s print '> ' gets.chomp end

    def say(s) puts s end def stop end
  16. 41 first_name = ask "What is your first name?" say

    "Hi, #{first_name}." last_name = ask "What is your last name?" say "Got it -- #{last_name}!" say "Thanks for your help, #{first_name}!" say "Bye-bye for now!" stop
  17. 42 wizard = Fiber.new do first_name = ask "What is

    your first name?" say "Hi, #{first_name}." last_name = ask "What is your last name?" say "Got it -- #{last_name}!" say "Thanks for your help, #{first_name}!" say "Bye-bye for now!" stop end
  18. 44 answer = nil loop do instruction = wizard.resume(answer) case

    instruction when Question when Statement when Stop end end
  19. 45 answer = nil loop do instruction = wizard.resume(answer) case

    instruction when Question puts instruction.text print '> ' answer = gets.chomp when Statement when Stop end end
  20. 46 answer = nil loop do instruction = wizard.resume(answer) case

    instruction when Question puts instruction.text print '> ' answer = gets.chomp when Statement puts instruction.text when Stop end end
  21. 47 answer = nil loop do instruction = wizard.resume(answer) case

    instruction when Question puts instruction.text print '> ' answer = gets.chomp when Statement puts instruction.text when Stop break end end
  22. 49 pieces = [] answer = nil loop do instruction

    = wizard.resume(answer) case instruction when Question when Statement when Stop end end
  23. 50 when Question pieces << instruction.text pieces << '<form method="post"

    action="/">' pieces << '<input type="text" name="answer">' pieces << '<input type="submit">' pieces << '</form>' return pieces.join(' ')
  24. 57 handle_exception do puts 'before' puts Boom.new.throw puts 'after' end.on(Boom)

    do |e| e.resume('exceptional') end.run before exceptional after
  25. 58 handle_exception do puts 'before' puts Boom.new.throw puts 'after' end.run

    before sample.rb:31:
 in `block in run': ohnoes kaboom
 (Boom)
  26. 63 Create a fiber Fiber.new { … } Start running

    a fiber f.resume(…) Resume an running fiber f.resume(…) Return from a fiber Fiber.yield(…)