def initialize(name) 5 @name = name 6 end 7 8 def set status(status) 9 @status = status 10 end 11 12 def report 13 ”#{@name} is #{@status}” 14 end 15 end 8 di 28
Star.new ’Vin Diesel’ ⇒ #<Celluloid::Actor(Star:0x3ffd25db3a3c) @ 4 vin.set status ’driving a car!’ ⇒ ” driving a car!” 5 vin.report ⇒ ” Vin Diesel is driving a car!” 9 di 28
= Star.new ’Richard B. Riddick’ 5 riddick.set status ’killing some Necromongers’ 6 riddick.set status ’leading the Necromongers’ 7 f = riddick.future :report 8 puts f.inspect 9 #do something 10 puts f.value 1 #<Celluloid::Future:0x007faad50c35c8> 2 Richard B. Riddick is leading the Necromongers 13 di 28
riddick.set status ’getting bored!’ 14 riddick.report 15 end ⇒ #<Celluloid::Future:0x007feabb3ddb48> 16 17 riddick.report ⇒ ” Richard B. Riddick is leading the Necromongers” 18 future.value ⇒ ” Richard B. Riddick is getting bored!” 14 di 28
3 class CannotWinError < StandardError; end 4 5 def initialize 6 puts ”Spawning a NecroMonger!” 7 end 8 9 def fight(who) 10 puts ”Fighting against #{who}” 11 raise CannotWinError, ” Riddick is too strong!”if who == :riddick 12 end 13 end 16 di 28
do 17 supervisor.actor.fight! :someone 18 supervisor.actor.fight! :riddick 19 sleep 3 20 end 1 Spawning a NecroMonger! 2 Fighting against someone 3 Fighting against riddick 4 Spawning a NecroMonger! 5 Fighting against someone 6 Fighting against riddick 7 Spawning a NecroMonger! 17 di 28
15 if (count assets[:NecroMongerFighter] || 0) < 3 16 lord marshal = Actor[:lord marshal] 17 3.times { lord marshal.link NecroMongerFighter.new } 18 else 19 p ” We have enough troops Lord Marshal!” 20 end 21 end 22 end 23 di 28
3 trap exit :necromonger died 4 5 def necromonger died(actor, reason) 6 p ” Damn Riddick! #{actor.inspect} has died because of a #{rea- son.class}” 7 Actor[:logistic].prepare reinforcements! 8 end 9 end 24 di 28
execution 18 wait :finished 19 end 20 end 21 22 main = MainGroup.run! 23 puts main.actor.wait backgroud execution 1 Spawning a NecroMonger! 2 {:NecroMongerFighter=>1, :NecroMonger=>1} 3 ”Damn Riddick! #<Celluloid::Actor(NecroMonger) dead> has died because o 4 Spawning a NecroMonger! 5 Spawning a NecroMonger! 6 Spawning a NecroMonger! 7 {:NecroMongerFighter=>3, :NecroMonger=>3} 26 di 28
Caiazza This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. http://creativecommons.org/licenses/by-sa/3.0/ Sources This work is typesetted with L A TEX and RubySocialClub gem. Sources are available at https://bitbucket.org/nolith/celluloid-presentation 28 di 28