class Celluloid::RingBuffer - celluloid-0.15.2 Documentation

# class Celluloid::RingBuffer

### Public Class Methods

new(size) click to toggle source

    # File lib/celluloid/logging/ring_buffer.rb, line 3
    def initialize(size)
      @size = size
      @start = 0
      @count = 0
      @buffer = Array.new(size)
      @mutex = Mutex.new
    end

### Public Instance Methods

<<(value)
Alias for: push
clear() click to toggle source

    # File lib/celluloid/logging/ring_buffer.rb, line 49
    def clear
      @buffer = Array.new(@size)
      @start = 0
      @count = 0
    end

empty?() click to toggle source

    # File lib/celluloid/logging/ring_buffer.rb, line 15
    def empty?
      @count == 0
    end

flush() click to toggle source

    # File lib/celluloid/logging/ring_buffer.rb, line 39
    def flush
      values = []
      @mutex.synchronize do
        while !empty?
          values << remove_element
        end
      end
      values
    end

full?() click to toggle source

    # File lib/celluloid/logging/ring_buffer.rb, line 11
    def full?
      @count == @size
    end

push(value) click to toggle source

    # File lib/celluloid/logging/ring_buffer.rb, line 19
    def push(value)
      @mutex.synchronize do
        stop = (@start + @count) % @size
        @buffer[stop] = value
        if full?
          @start = (@start + 1) % @size
        else
          @count += 1
        end
        value
      end
    end

Also aliased as: <<
shift() click to toggle source

    # File lib/celluloid/logging/ring_buffer.rb, line 33
    def shift
      @mutex.synchronize do
        remove_element
      end
    end

### Private Instance Methods

remove_element() click to toggle source

    # File lib/celluloid/logging/ring_buffer.rb, line 57
    def remove_element
      return nil if empty?
      value, @buffer[@start] = @buffer[@start], nil
      @start = (@start + 1) % @size
      @count -= 1
      value
    end