z, ? | toggle help (this) |
space, → | next slide |
shift-space, ← | previous slide |
d | toggle debug mode |
## <ret> | go to slide # |
c, t | table of contents (vi) |
f | toggle footer |
r | reload slides |
n | toggle notes |
p | run preshow |
P | toggle pause |
I'm sorry that I long ago coined the
term "objects" for this topic because
it gets many people to focus on the
lesser idea.
The big idea is "messaging" --
that is what the kernel of Smalltalk/Squeak
is all about.
-- Alan Kay
Erlang might be the only object oriented
language because the 3 tenets of object
oriented programming are that it's based
on message passing, that you have isolation
between objects and have polymorphism.
-- Joe Armstrong
class Duck
include Celluloid
def quack
"Quaaaaaack!"
end
end
>> d = Duck.new
=> #<Celluloid::ActorProxy(Duck:0x5260)>
>> d.quack
=> "Quaaaaaack!"
class SlowDuck
include Celluloid
def quack
sleep 10
"Quaaaaaack!"
end
end
>> d = SlowDuck.new
=> #<Celluloid::ActorProxy(Duck:0x5458)>
>> d.quack
...
=> "Quaaaaaack!"
>> d = SlowDuck.new
=> #<Celluloid::ActorProxy(Duck:0x5458)>
>> d.async.quack
=> nil
>> d = SlowDuck.new
=> #<Celluloid::ActorProxy(Duck:0x5458)>
>> q = d.future.quack
>> q.value # Blocking call
=> "Quaaaaaack!"
class Duckling
include Celluloid
def quack
raise RuntimeError, "Oops!"
end
end
class ParentDuck
include Celluloid
def initialize
@baby = Duckling.new_link
end
def quack
@baby.async.quack
end
end
>> p = ParentDuck.new
>> p.quack
...
E, [...] ERROR -- : Duckling crashed!
...
E, [...] ERROR -- : ParentDuck crashed!
class Farmyard < Celluloid::SupervisionGroup
pool SlowDuck, as: :duck_family, size: 5
end
>> Farmyard.run!
>> f = (0..7).to_a.map { |n|
Celluloid::Actor[:duck_family].future.quack }
>> f.map(&:value)
=> ["Quaaaaaack!", "Quaaaaaack!", "Quaaaaaack!",
"Quaaaaaack!", "Quaaaaaack!", "Quaaaaaack!",
"Quaaaaaack!", "Quaaaaaack!"]
class DuckServer
include Celluloid::IO
finalizer :shutdown
def initialize
@server = TCPServer.new('localhost', 3000)
async.run
end
def shutdown
@server.close if @server
end
def run
loop { async.handle_connection @server.accept }
end
def handle_connection(socket)
loop do
socket.readpartial(4096)
socket.puts "Quaaaaaack!"
end
rescue EOFError
socket.close
end
end
>> s = DuckServer.new