Module: DiscreteEvent::Events

Defined in:
lib/discrete_event/events.rb

Overview

Mix-in for simulations with multiple objects that have to share the same clock. See the README for an example.

The implementing class must have an instance method event_queue that returns the EventQueue to use; this method may be private.

Instance Method Summary collapse

Instance Method Details

#after(delay) { ... } ⇒ Event

Parameters:

  • delay (Number)

    after which action should run; non-negative

Yields:

  • action to be run after delay

Returns:

  • (Event)


34
35
36
# File 'lib/discrete_event/events.rb', line 34

def after(delay, &action)
  event_queue.after(delay, &action)
end

#at(time) { ... } ⇒ Event

Parameters:

  • time (Number)

    at which action should run; must be >= #now

Yields:

  • action to be run at time

Returns:

  • (Event)


21
22
23
# File 'lib/discrete_event/events.rb', line 21

def at(time, &action)
  event_queue.at(time, &action)
end

#at_each(elements, time = nil) {|element| ... } ⇒ nil

Parameters:

  • elements (Enumerable)

    to yield; must be in ascending order according to time; note that this method keeps a reference to this object and removes elements as they are executed, so you may want to pass a copy if you plan to change it after this call returns

  • time (Proc, Symbol, nil) (defaults to: nil)

    used to determine when the action will run for a given element; if a Proc, the proc must return the appropriate time; if a Symbol, each element must respond to time; if nil, it is assumed that element.time returns the time

Yields:

  • (element)

Yield Parameters:

  • element (Object)

    from elements

Returns:

  • (nil)


68
69
70
# File 'lib/discrete_event/events.rb', line 68

def at_each(elements, time = nil, &action)
  event_queue.at_each(elements, time, &action)
end

#cancel(event) ⇒ nil

Returns:

  • (nil)


43
44
45
# File 'lib/discrete_event/events.rb', line 43

def cancel(event)
  event_queue.cancel(event)
end

#every(interval, start = 0, &action) ⇒ nil

Parameters:

  • interval (Numeric)

    non-negative

  • start (Numeric) (defaults to: 0)

    block first runs at this time

Returns:

  • (nil)


91
92
93
# File 'lib/discrete_event/events.rb', line 91

def every(interval, start = 0, &action)
  event_queue.every(interval, start, &action)
end

#nowNumber

Returns:

  • (Number)


100
101
102
# File 'lib/discrete_event/events.rb', line 100

def now
  event_queue.now
end

#recur_after(interval) ⇒ nil

Parameters:

  • interval (Number)

    non-negative

Returns:

  • (nil)


79
80
81
# File 'lib/discrete_event/events.rb', line 79

def recur_after(interval)
  event_queue.recur_after(interval)
end