Sha256: 2fb87335edc6a6d9c23bc683260455696283e2aa4a58dd9b7c57ee658bd417e8

Contents?: true

Size: 1015 Bytes

Versions: 2

Compression:

Stored size: 1015 Bytes

Contents

#!/usr/bin/env ruby

$LOAD_PATH.push File.expand_path("../lib", __dir__)
require "celluloid/autostart"

# This example builds on basic_usage.rb to show two things about #async: the
# (new) fluent API and the preservation of causality order.
class Stack
  include Celluloid

  attr_reader :ary

  def initialize
    @ary = []
  end

  def push(x)
    @ary.push x
  end
  alias << push

  def pop
    @ary.pop
  end

  def show
    p @ary
  end
end

st = Stack.new

# Schedule three calls to #push some integers on the stack. They will execute
# in order because the calls originated as a sequence of method calls in a
# single thread.
st.async << 1 << 2 << 3

# Schedule a call to show the stack after the three push calls execute.
st.async.show

# Schedule three calls to #pop from the stack.
st.async.pop.pop.pop

# The next (non-async) call is guaranteed to execute after methods previously
# scheduled in this thread. The causal order of calls (order as requested) is
# preserved in the execution order.
st.show

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
celluloid-0.18.0 examples/stack.rb
celluloid-0.18.0.pre2 examples/stack.rb