Rename HistoryArray to Ring

`HistoryArray` is a very specific name and it doesn't tell the reader what
it *really* means unless you read its code or the docs of the class.

On the other hand, `Ring` is a [very well-known term][1], which means exactly
what `HistoryArray` does. The alias name for it is circular buffer. I chose
`Ring` because it is shorter and used by Golang, so I expect programmers to
be familiar with `Ring`.

[1]: https://en.wikipedia.org/wiki/Circular_buffer
This commit is contained in:
Kyrylo Silin 2018-10-19 22:03:41 +08:00
parent d37d0c0489
commit 2519818cc9
5 changed files with 29 additions and 29 deletions

View File

@ -144,7 +144,7 @@ require 'pathname'
require 'pry/version'
require 'pry/repl'
require 'pry/code'
require 'pry/history_array'
require 'pry/ring'
require 'pry/helpers'
require 'pry/code_object'
require 'pry/method'

View File

@ -72,8 +72,8 @@ class Pry
@config = Pry::Config.new
config.merge!(options)
push_prompt(config.prompt)
@input_array = Pry::HistoryArray.new config.memory_size
@output_array = Pry::HistoryArray.new config.memory_size
@input_array = Pry::Ring.new(config.memory_size)
@output_array = Pry::Ring.new(config.memory_size)
@custom_completions = config.command_completions
set_last_result nil
@input_array << nil
@ -177,8 +177,8 @@ class Pry
undef :memory_size= if method_defined? :memory_size=
def memory_size=(size)
@input_array = Pry::HistoryArray.new(size)
@output_array = Pry::HistoryArray.new(size)
@input_array = Pry::Ring.new(size)
@output_array = Pry::Ring.new(size)
end
# Inject all the sticky locals into the current binding.

View File

@ -1,19 +1,19 @@
class Pry
# A history array is an array to which you can only add elements. Older
# entries are removed progressively, so that the array never contains more than
# N elements.
# A ring is an array to which you can only add elements. Older entries are
# removed progressively, so that the array never contains more than N
# elements.
#
# History arrays are used by Pry to store the output of the last commands.
# Rings 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
class HistoryArray
# ring = Pry::Ring.new(10)
# ring << 1 << 2 << 3
# ring[0] # => 1
# ring[1] # => 2
# 10.times { |n| ring << n }
# ring[0] # => nil
# ring[-1] # => 9
class Ring
include Enumerable
# @param [Integer] size Maximum amount of objects in the array

View File

@ -273,7 +273,7 @@ describe Pry do
t.eval "42"
res = t.eval "_out_"
expect(res).to be_a_kind_of Pry::HistoryArray
expect(res).to be_a_kind_of(Pry::Ring)
expect(res[1..2]).to eq [:foo, 42]
end
@ -283,7 +283,7 @@ describe Pry do
t.eval "42"
res = t.eval "_in_"
expect(res).to be_a_kind_of Pry::HistoryArray
expect(res).to be_a_kind_of(Pry::Ring)
expect(res[1..2]).to eq [":foo\n", "42\n"]
end

View File

@ -1,13 +1,13 @@
require_relative 'helper'
describe Pry::HistoryArray do
describe Pry::Ring do
before do
@array = Pry::HistoryArray.new 10
@populated = @array.dup << 1 << 2 << 3 << 4
@ring = Pry::Ring.new(10)
@populated = @ring.dup << 1 << 2 << 3 << 4
end
it 'should have a maximum size specifed at creation time' do
expect(@array.max_size).to eq 10
expect(@ring.max_size).to eq 10
end
it 'should be able to be added objects to' do
@ -48,16 +48,16 @@ describe Pry::HistoryArray do
end
it 'should remove older entries' do
11.times { |n| @array << n }
11.times { |n| @ring << n }
expect(@array[0]).to eq nil
expect(@array[1]).to eq 1
expect(@array[10]).to eq 10
expect(@ring[0]).to eq nil
expect(@ring[1]).to eq 1
expect(@ring[10]).to eq 10
end
it 'should not be larger than specified maximum size' do
12.times { |n| @array << n }
expect(@array.entries.compact.size).to eq 10
12.times { |n| @ring << n }
expect(@ring.entries.compact.size).to eq 10
end
it 'should pop!' do