class Pry::HistoryArray

A history array is an array to which you can only add elements. Older entries are removed progressively, so that the aray never contains more than N elements.

History arrays are used by Pry to store the output of the last commands.

@example

ary = Pry::HistoryArray.new 10
ary << 1 << 2 << 3
ary[0] # => 1
ary[1] # => 2
10.times { |n| ary << n }
ary[0] # => nil
ary[-1] # => 9

Attributes

max_size[R]

@return [Integer] Maximum amount of objects in the array

Public Class Methods

new(size) click to toggle source

@param [Integer] size Maximum amount of objects in the array

# File lib/pry/history_array.rb, line 20
def initialize(size)
  @max_size = size

  @hash  = {}
  @count = 0
end

Public Instance Methods

<<(value) click to toggle source

Pushes an object at the end of the array @param [Object] value Object to be added

# File lib/pry/history_array.rb, line 29
def <<(value)
  @hash[@count] = value

  if @hash.size > max_size
    @hash.delete(@count - max_size)
  end

  @count += 1

  self
end
[](index_or_range, size = nil) click to toggle source

@overload [](index)

@param [Integer] index Index of the item to access.
@return [Object, nil] Item at that index or nil if it has been removed.

@overload [](index, size)

@param [Integer] index Index of the first item to access.
@param [Integer] size Amount of items to access
@return [Array, nil] The selected items. Nil if index is greater than
  the size of the array.

@overload [](range)

@param [Range<Integer>] range Range of indices to access.
@return [Array, nil] The selected items. Nil if index is greater than
  the size of the array.
# File lib/pry/history_array.rb, line 53
def [](index_or_range, size = nil)
  if index_or_range.is_a? Integer
    index = convert_index(index_or_range)

    if size
      end_index = index + size
      index > @count ? nil : (index...[end_index, @count].min).map do |n|
        @hash[n]
      end
    else
      @hash[index]
    end
  else
    range = convert_range(index_or_range)
    range.begin > @count ? nil : range.map { |n| @hash[n] }
  end
end
count() click to toggle source
Alias for: size
each() { |hash| ... } click to toggle source
# File lib/pry/history_array.rb, line 82
def each
  ((@count - size)...@count).each do |n|
    yield @hash[n]
  end
end
empty?() click to toggle source
# File lib/pry/history_array.rb, line 78
def empty?
  size == 0
end
inspect() click to toggle source
# File lib/pry/history_array.rb, line 97
def inspect
  "#<#{self.class} size=#{size} first=#{@count - size} max_size=#{max_size}>"
end
length() click to toggle source
Alias for: size
pop!() click to toggle source
# File lib/pry/history_array.rb, line 92
def pop!
  @hash.delete @count - 1
  @count -= 1
end
size() click to toggle source

@return [Integer] Amount of objects in the array

# File lib/pry/history_array.rb, line 72
def size
  @count
end
Also aliased as: count, length
to_a() click to toggle source
# File lib/pry/history_array.rb, line 88
def to_a
  ((@count - size)...@count).map { |n| @hash[n] }
end