Class: Pry::HistoryArray

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/pry/history_array.rb

Overview

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.

Examples:

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (HistoryArray) initialize(size)

A new instance of HistoryArray

Parameters:

  • size (Integer)

    Maximum amount of objects in the array



20
21
22
23
24
25
# File 'lib/pry/history_array.rb', line 20

def initialize(size)
  @max_size = size

  @hash  = {}
  @count = 0
end

Instance Attribute Details

- (Integer) max_size (readonly)

Maximum amount of objects in the array

Returns:

  • (Integer)

    Maximum amount of objects in the array



91
92
93
# File 'lib/pry/history_array.rb', line 91

def max_size
  @max_size
end

Instance Method Details

- (Object) <<(value)

Pushes an object at the end of the array

Parameters:

  • value (Object)

    Object to be added



29
30
31
32
33
34
35
36
37
38
39
# 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

- (Object?) [](index) - (Array?) [](index, size) - (Array?) [](range)

Overloads:

  • - (Object?) [](index)

    Item at that index or nil if it has been removed.

    Parameters:

    • index (Integer)

      Index of the item to access.

    Returns:

    • (Object, nil)

      Item at that index or nil if it has been removed.

  • - (Array?) [](index, size)

    The selected items. Nil if index is greater than the size of the array.

    Parameters:

    • index (Integer)

      Index of the first item to access.

    • size (Integer)

      Amount of items to access

    Returns:

    • (Array, nil)

      The selected items. Nil if index is greater than the size of the array.

  • - (Array?) [](range)

    The selected items. Nil if index is greater than the size of the array.

    Parameters:

    • range (Range<Integer>)

      Range of indices to access.

    Returns:

    • (Array, nil)

      The selected items. Nil if index is greater than the size of the array.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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

- (Object) convert_index(n) (private)



94
95
96
# File 'lib/pry/history_array.rb', line 94

def convert_index(n)
  n >= 0 ? n : @count + n
end

- (Object) convert_range(range) (private)



98
99
100
101
102
103
# File 'lib/pry/history_array.rb', line 98

def convert_range(range)
  end_index = convert_index(range.end)
  end_index += 1 unless range.exclude_end?

  Range.new(convert_index(range.begin), [end_index, @count].min, true)
end

- (Object) each



76
77
78
79
80
# File 'lib/pry/history_array.rb', line 76

def each
  ((@count - size)...@count).each do |n|
    yield @hash[n]
  end
end

- (Object) inspect



86
87
88
# File 'lib/pry/history_array.rb', line 86

def inspect
  "#<#{self.class} size=#{size} first=#{@count - size} max_size=#{max_size}>"
end

- (Integer) size

Amount of objects in the array

Returns:

  • (Integer)

    Amount of objects in the array



72
73
74
# File 'lib/pry/history_array.rb', line 72

def size
  @count
end

- (Object) to_a



82
83
84
# File 'lib/pry/history_array.rb', line 82

def to_a
  ((@count - size)...@count).map { |n| @hash[n] }
end