Guest User

Untitled

a guest
Jan 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. require 'eventmachine'
  2.  
  3. class Actor
  4. def initialize(&cb)
  5. @channel = EM::Channel.new
  6. @sid = @channel.subscribe &cb
  7. end
  8. def << message
  9. @channel.push message
  10. end
  11. def die
  12. @channel.unsubscribe(@sid)
  13. end
  14. end
  15.  
  16. class Player < Actor
  17. def initialize(output)
  18. cb = lambda do |message|
  19. if message == "play"
  20. puts "playing #{output}"
  21. end
  22. end
  23. super &cb
  24. end
  25. end
  26.  
  27.  
  28.  
  29.  
  30.  
  31. EM::run {
  32. rock_n_roll = Player.new("AC/DC")
  33. classical = Player.new("Mozart")
  34. rock_n_roll << "play"
  35. classical << "play"
  36. EM::stop_event_loop
  37. }
Add Comment
Please, Sign In to add comment