pry 0.11.3-java → 0.12.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +139 -1
  3. data/LICENSE +1 -1
  4. data/README.md +13 -30
  5. data/bin/pry +0 -4
  6. data/lib/pry.rb +17 -47
  7. data/lib/pry/cli.rb +17 -24
  8. data/lib/pry/code.rb +6 -6
  9. data/lib/pry/code/code_file.rb +5 -4
  10. data/lib/pry/code/code_range.rb +3 -3
  11. data/lib/pry/code/loc.rb +14 -8
  12. data/lib/pry/code_object.rb +4 -4
  13. data/lib/pry/color_printer.rb +1 -0
  14. data/lib/pry/command.rb +36 -29
  15. data/lib/pry/command_set.rb +17 -52
  16. data/lib/pry/commands/amend_line.rb +3 -4
  17. data/lib/pry/commands/bang.rb +1 -1
  18. data/lib/pry/commands/cat.rb +7 -6
  19. data/lib/pry/commands/cat/exception_formatter.rb +9 -8
  20. data/lib/pry/commands/cat/input_expression_formatter.rb +1 -1
  21. data/lib/pry/commands/change_prompt.rb +29 -9
  22. data/lib/pry/commands/clear_screen.rb +14 -0
  23. data/lib/pry/commands/code_collector.rb +9 -9
  24. data/lib/pry/commands/easter_eggs.rb +3 -3
  25. data/lib/pry/commands/edit.rb +8 -7
  26. data/lib/pry/commands/exit.rb +2 -1
  27. data/lib/pry/commands/find_method.rb +11 -13
  28. data/lib/pry/commands/gem_cd.rb +1 -1
  29. data/lib/pry/commands/gem_install.rb +2 -2
  30. data/lib/pry/commands/gem_list.rb +2 -2
  31. data/lib/pry/commands/gem_open.rb +1 -1
  32. data/lib/pry/commands/gem_search.rb +2 -2
  33. data/lib/pry/commands/gem_stats.rb +83 -0
  34. data/lib/pry/commands/gist.rb +7 -6
  35. data/lib/pry/commands/help.rb +3 -3
  36. data/lib/pry/commands/hist.rb +9 -8
  37. data/lib/pry/commands/import_set.rb +2 -1
  38. data/lib/pry/commands/install_command.rb +7 -6
  39. data/lib/pry/commands/list_inspectors.rb +2 -2
  40. data/lib/pry/commands/ls.rb +27 -30
  41. data/lib/pry/commands/ls/constants.rb +4 -4
  42. data/lib/pry/commands/ls/formatter.rb +3 -2
  43. data/lib/pry/commands/ls/globals.rb +0 -2
  44. data/lib/pry/commands/ls/grep.rb +0 -2
  45. data/lib/pry/commands/ls/instance_vars.rb +0 -1
  46. data/lib/pry/commands/ls/local_names.rb +0 -2
  47. data/lib/pry/commands/ls/local_vars.rb +0 -2
  48. data/lib/pry/commands/ls/ls_entity.rb +0 -1
  49. data/lib/pry/commands/ls/methods.rb +0 -3
  50. data/lib/pry/commands/ls/methods_helper.rb +1 -1
  51. data/lib/pry/commands/ls/self_methods.rb +0 -1
  52. data/lib/pry/commands/play.rb +1 -2
  53. data/lib/pry/commands/pry_backtrace.rb +1 -1
  54. data/lib/pry/commands/raise_up.rb +2 -1
  55. data/lib/pry/commands/ri.rb +5 -4
  56. data/lib/pry/commands/shell_command.rb +3 -2
  57. data/lib/pry/commands/shell_mode.rb +6 -6
  58. data/lib/pry/commands/show_doc.rb +5 -7
  59. data/lib/pry/commands/show_info.rb +25 -18
  60. data/lib/pry/commands/show_source.rb +5 -2
  61. data/lib/pry/commands/stat.rb +1 -1
  62. data/lib/pry/commands/watch_expression.rb +9 -7
  63. data/lib/pry/commands/whereami.rb +4 -4
  64. data/lib/pry/commands/wtf.rb +15 -2
  65. data/lib/pry/config.rb +33 -9
  66. data/lib/pry/config/behavior.rb +229 -205
  67. data/lib/pry/config/convenience.rb +24 -21
  68. data/lib/pry/config/default.rb +153 -143
  69. data/lib/pry/config/memoization.rb +41 -37
  70. data/lib/pry/core_extensions.rb +4 -3
  71. data/lib/pry/editor.rb +5 -12
  72. data/lib/pry/exceptions.rb +0 -2
  73. data/lib/pry/helpers.rb +1 -0
  74. data/lib/pry/helpers/base_helpers.rb +133 -4
  75. data/lib/pry/helpers/command_helpers.rb +5 -4
  76. data/lib/pry/helpers/documentation_helpers.rb +2 -2
  77. data/lib/pry/helpers/options_helpers.rb +5 -5
  78. data/lib/pry/helpers/platform.rb +58 -0
  79. data/lib/pry/helpers/table.rb +20 -15
  80. data/lib/pry/helpers/text.rb +3 -4
  81. data/lib/pry/history.rb +21 -8
  82. data/lib/pry/hooks.rb +3 -3
  83. data/lib/pry/indent.rb +15 -17
  84. data/lib/pry/input_completer.rb +12 -7
  85. data/lib/pry/input_lock.rb +0 -2
  86. data/lib/pry/last_exception.rb +1 -1
  87. data/lib/pry/method.rb +37 -31
  88. data/lib/pry/method/disowned.rb +2 -1
  89. data/lib/pry/method/patcher.rb +2 -2
  90. data/lib/pry/method/weird_method_locator.rb +7 -8
  91. data/lib/pry/object_path.rb +5 -4
  92. data/lib/pry/output.rb +3 -2
  93. data/lib/pry/pager.rb +4 -3
  94. data/lib/pry/platform.rb +79 -81
  95. data/lib/pry/plugins.rb +7 -3
  96. data/lib/pry/prompt.rb +144 -25
  97. data/lib/pry/pry_class.rb +53 -29
  98. data/lib/pry/pry_instance.rb +88 -55
  99. data/lib/pry/repl.rb +33 -4
  100. data/lib/pry/repl_file_loader.rb +1 -2
  101. data/lib/pry/ring.rb +84 -0
  102. data/lib/pry/rubygem.rb +6 -6
  103. data/lib/pry/slop.rb +17 -17
  104. data/lib/pry/slop/commands.rb +3 -4
  105. data/lib/pry/slop/option.rb +19 -21
  106. data/lib/pry/terminal.rb +2 -1
  107. data/lib/pry/testable/mockable.rb +2 -2
  108. data/lib/pry/testable/pry_tester.rb +1 -1
  109. data/lib/pry/testable/utility.rb +2 -2
  110. data/lib/pry/testable/variables.rb +1 -1
  111. data/lib/pry/version.rb +1 -1
  112. data/lib/pry/wrapped_module.rb +15 -15
  113. data/lib/pry/wrapped_module/candidate.rb +2 -2
  114. metadata +19 -31
  115. data/lib/pry/commands/list_prompts.rb +0 -35
  116. data/lib/pry/commands/simple_prompt.rb +0 -22
  117. data/lib/pry/history_array.rb +0 -121
  118. data/lib/pry/rbx_path.rb +0 -22
@@ -8,7 +8,7 @@ module Pry::Testable::Variables
8
8
  # Foo # => NameError
9
9
  # Bar # => NameError
10
10
  #
11
- # @param [Array<Symbol>] *names
11
+ # @param [Array<Symbol>] names
12
12
  # An array of constant names that be defined by a block,
13
13
  # and removed by this method afterwards.
14
14
  #
@@ -1,3 +1,3 @@
1
1
  class Pry
2
- VERSION = "0.11.3"
2
+ VERSION = '0.12.0'
3
3
  end
@@ -26,7 +26,7 @@ class Pry
26
26
  # @return [Module, nil] The module or `nil` (if conversion failed).
27
27
  # @example
28
28
  # Pry::WrappedModule.from_str("Pry::Code")
29
- def self.from_str(mod_name, target=TOPLEVEL_BINDING)
29
+ def self.from_str(mod_name, target = TOPLEVEL_BINDING)
30
30
  if safe_to_evaluate?(mod_name, target)
31
31
  Pry::WrappedModule.new(target.eval(mod_name))
32
32
  else
@@ -49,6 +49,7 @@ class Pry
49
49
  def safe_to_evaluate?(str, target)
50
50
  return true if str.strip == "self"
51
51
  return false if str =~ /%/
52
+
52
53
  kind = target.eval("defined?(#{str})")
53
54
  kind =~ /variable|constant/
54
55
  end
@@ -58,6 +59,7 @@ class Pry
58
59
  # @param [Module] mod
59
60
  def initialize(mod)
60
61
  raise ArgumentError, "Tried to initialize a WrappedModule with a non-module #{mod.inspect}" unless ::Module === mod
62
+
61
63
  @wrapped = mod
62
64
  @memoized_candidates = []
63
65
  @host_file_lines = nil
@@ -109,10 +111,6 @@ class Pry
109
111
  def singleton_class?
110
112
  if Pry::Method.safe_send(wrapped, :respond_to?, :singleton_class?)
111
113
  Pry::Method.safe_send(wrapped, :singleton_class?)
112
- elsif defined?(Rubinius)
113
- # https://github.com/rubinius/rubinius/commit/2e71722dba53d1a92c54d5e3968d64d1042486fe singleton_class? added 30 Jul 2014
114
- # https://github.com/rubinius/rubinius/commit/4310f6b2ef3c8fc88135affe697db4e29e4621c4 has been around since 2011
115
- !!Rubinius::Type.singleton_class_object(wrapped)
116
114
  else
117
115
  wrapped != Pry::Method.safe_send(wrapped, :ancestors).first
118
116
  end
@@ -138,10 +136,10 @@ class Pry
138
136
  def singleton_instance
139
137
  raise ArgumentError, "tried to get instance of non singleton class" unless singleton_class?
140
138
 
141
- if Helpers::BaseHelpers.jruby?
139
+ if Helpers::Platform.jruby?
142
140
  wrapped.to_java.attached
143
141
  else
144
- @singleton_instance ||= ObjectSpace.each_object(wrapped).detect{ |x| (class << x; self; end) == wrapped }
142
+ @singleton_instance ||= ObjectSpace.each_object(wrapped).detect { |x| (class << x; self; end) == wrapped }
145
143
  end
146
144
  end
147
145
 
@@ -150,7 +148,7 @@ class Pry
150
148
  wrapped.send(method_name, *args, &block)
151
149
  end
152
150
 
153
- def respond_to?(method_name, include_all=false)
151
+ def respond_to?(method_name, include_all = false)
154
152
  super || wrapped.respond_to?(method_name, include_all)
155
153
  end
156
154
 
@@ -253,7 +251,7 @@ class Pry
253
251
  y.yield candidate(num)
254
252
  end
255
253
  end
256
- Pry::Helpers::BaseHelpers.jruby_19? ? enum.to_a : enum
254
+ Helpers::Platform.jruby_19? ? enum.to_a : enum
257
255
  end
258
256
 
259
257
  # @return [Boolean] Whether YARD docs are available for this module.
@@ -267,7 +265,7 @@ class Pry
267
265
  # When `self` is a `Module` then return the
268
266
  # nth ancestor, otherwise (in the case of classes) return the
269
267
  # nth ancestor that is a class.
270
- def super(times=1)
268
+ def super(times = 1)
271
269
  return self if times.zero?
272
270
 
273
271
  if wrapped.is_a?(Class)
@@ -302,7 +300,7 @@ class Pry
302
300
  # speed up source code extraction.
303
301
  def method_candidates
304
302
  @method_candidates ||= all_source_locations_by_popularity.map do |group|
305
- methods_sorted_by_source_line = group.last.sort_by(&:source_line)
303
+ methods_sorted_by_source_line = group.last.sort_by(&:source_line)
306
304
  [methods_sorted_by_source_line.first, methods_sorted_by_source_line.last]
307
305
  end
308
306
  end
@@ -315,7 +313,7 @@ class Pry
315
313
  @all_source_locations_by_popularity = ims.group_by { |v| Array(v.source_location).first }.
316
314
  sort_by do |path, methods|
317
315
  expanded = File.expand_path(path)
318
- load_order = $LOADED_FEATURES.index{ |file| expanded.end_with?(file) }
316
+ load_order = $LOADED_FEATURES.index { |file| expanded.end_with?(file) }
319
317
 
320
318
  [-methods.size, load_order || (1.0 / 0.0)]
321
319
  end
@@ -323,16 +321,16 @@ class Pry
323
321
 
324
322
  # We only want methods that have a non-nil `source_location`. We also
325
323
  # skip some spooky internal methods.
326
- # (i.e we skip `__class_init__` because it's an odd rbx specific thing that causes tests to fail.)
324
+ #
327
325
  # @return [Array<Pry::Method>]
328
326
  def all_relevant_methods_for(mod)
329
327
  methods = all_methods_for(mod).select(&:source_location).
330
- reject{ |x| x.name == '__class_init__' || method_defined_by_forwardable_module?(x) }
328
+ reject { |x| method_defined_by_forwardable_module?(x) }
331
329
 
332
330
  return methods unless methods.empty?
333
331
 
334
332
  safe_send(mod, :constants).flat_map do |const_name|
335
- if const = nested_module?(mod, const_name)
333
+ if (const = nested_module?(mod, const_name))
336
334
  all_relevant_methods_for(const)
337
335
  else
338
336
  []
@@ -349,9 +347,11 @@ class Pry
349
347
 
350
348
  def nested_module?(parent, name)
351
349
  return if safe_send(parent, :autoload?, name)
350
+
352
351
  child = safe_send(parent, :const_get, name)
353
352
  return unless Module === child
354
353
  return unless safe_send(child, :name) == "#{safe_send(parent, :name)}::#{name}"
354
+
355
355
  child
356
356
  end
357
357
 
@@ -2,7 +2,6 @@ require 'pry/helpers/documentation_helpers'
2
2
 
3
3
  class Pry
4
4
  class WrappedModule
5
-
6
5
  # This class represents a single candidate for a module/class definition.
7
6
  # It provides access to the source, documentation, line and file
8
7
  # for a monkeypatch (reopening) of a class/module.
@@ -64,6 +63,7 @@ class Pry
64
63
  # @return [String] The documentation for the candidate.
65
64
  def doc
66
65
  return nil if file.nil?
66
+
67
67
  @doc ||= get_comment_content(Pry::Code.from_file(file).comment_describing(line))
68
68
  end
69
69
 
@@ -76,7 +76,7 @@ class Pry
76
76
  file, line = first_method_source_location
77
77
  return nil if !file.is_a?(String)
78
78
 
79
- @source_location = [file, first_line_of_module_definition(file, line)]
79
+ @source_location = [file, first_line_of_module_definition(file, line)]
80
80
  rescue Pry::RescuableException
81
81
  nil
82
82
  end
metadata CHANGED
@@ -1,68 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.3
4
+ version: 0.12.0
5
5
  platform: java
6
6
  authors:
7
7
  - John Mair (banisterfiend)
8
8
  - Conrad Irwin
9
9
  - Ryan Fitzgerald
10
- autorequire:
10
+ - Kyrylo Silin
11
+ autorequire:
11
12
  bindir: bin
12
13
  cert_chain: []
13
- date: 2017-11-14 00:00:00.000000000 Z
14
+ date: 2018-11-04 00:00:00.000000000 Z
14
15
  dependencies:
15
16
  - !ruby/object:Gem::Dependency
16
- name: coderay
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
19
  - - "~>"
20
20
  - !ruby/object:Gem::Version
21
21
  version: 1.1.0
22
- type: :runtime
22
+ name: coderay
23
23
  prerelease: false
24
+ type: :runtime
24
25
  version_requirements: !ruby/object:Gem::Requirement
25
26
  requirements:
26
27
  - - "~>"
27
28
  - !ruby/object:Gem::Version
28
29
  version: 1.1.0
29
30
  - !ruby/object:Gem::Dependency
30
- name: method_source
31
31
  requirement: !ruby/object:Gem::Requirement
32
32
  requirements:
33
33
  - - "~>"
34
34
  - !ruby/object:Gem::Version
35
35
  version: 0.9.0
36
- type: :runtime
36
+ name: method_source
37
37
  prerelease: false
38
+ type: :runtime
38
39
  version_requirements: !ruby/object:Gem::Requirement
39
40
  requirements:
40
41
  - - "~>"
41
42
  - !ruby/object:Gem::Version
42
43
  version: 0.9.0
43
44
  - !ruby/object:Gem::Dependency
44
- name: bundler
45
- requirement: !ruby/object:Gem::Requirement
46
- requirements:
47
- - - "~>"
48
- - !ruby/object:Gem::Version
49
- version: '1.0'
50
- type: :development
51
- prerelease: false
52
- version_requirements: !ruby/object:Gem::Requirement
53
- requirements:
54
- - - "~>"
55
- - !ruby/object:Gem::Version
56
- version: '1.0'
57
- - !ruby/object:Gem::Dependency
58
- name: spoon
59
45
  requirement: !ruby/object:Gem::Requirement
60
46
  requirements:
61
47
  - - "~>"
62
48
  - !ruby/object:Gem::Version
63
49
  version: '0.0'
64
- type: :runtime
50
+ name: spoon
65
51
  prerelease: false
52
+ type: :runtime
66
53
  version_requirements: !ruby/object:Gem::Requirement
67
54
  requirements:
68
55
  - - "~>"
@@ -73,6 +60,7 @@ email:
73
60
  - jrmair@gmail.com
74
61
  - conrad.irwin@gmail.com
75
62
  - rwfitzge@gmail.com
63
+ - silin@kyrylo.org
76
64
  executables:
77
65
  - pry
78
66
  extensions: []
@@ -105,6 +93,7 @@ files:
105
93
  - lib/pry/commands/cd.rb
106
94
  - lib/pry/commands/change_inspector.rb
107
95
  - lib/pry/commands/change_prompt.rb
96
+ - lib/pry/commands/clear_screen.rb
108
97
  - lib/pry/commands/code_collector.rb
109
98
  - lib/pry/commands/disable_pry.rb
110
99
  - lib/pry/commands/disabled_commands.rb
@@ -123,6 +112,7 @@ files:
123
112
  - lib/pry/commands/gem_open.rb
124
113
  - lib/pry/commands/gem_readme.rb
125
114
  - lib/pry/commands/gem_search.rb
115
+ - lib/pry/commands/gem_stats.rb
126
116
  - lib/pry/commands/gist.rb
127
117
  - lib/pry/commands/help.rb
128
118
  - lib/pry/commands/hist.rb
@@ -130,7 +120,6 @@ files:
130
120
  - lib/pry/commands/install_command.rb
131
121
  - lib/pry/commands/jump_to.rb
132
122
  - lib/pry/commands/list_inspectors.rb
133
- - lib/pry/commands/list_prompts.rb
134
123
  - lib/pry/commands/ls.rb
135
124
  - lib/pry/commands/ls/constants.rb
136
125
  - lib/pry/commands/ls/formatter.rb
@@ -160,7 +149,6 @@ files:
160
149
  - lib/pry/commands/show_info.rb
161
150
  - lib/pry/commands/show_input.rb
162
151
  - lib/pry/commands/show_source.rb
163
- - lib/pry/commands/simple_prompt.rb
164
152
  - lib/pry/commands/stat.rb
165
153
  - lib/pry/commands/switch_to.rb
166
154
  - lib/pry/commands/toggle_color.rb
@@ -182,10 +170,10 @@ files:
182
170
  - lib/pry/helpers/command_helpers.rb
183
171
  - lib/pry/helpers/documentation_helpers.rb
184
172
  - lib/pry/helpers/options_helpers.rb
173
+ - lib/pry/helpers/platform.rb
185
174
  - lib/pry/helpers/table.rb
186
175
  - lib/pry/helpers/text.rb
187
176
  - lib/pry/history.rb
188
- - lib/pry/history_array.rb
189
177
  - lib/pry/hooks.rb
190
178
  - lib/pry/indent.rb
191
179
  - lib/pry/input_completer.rb
@@ -204,9 +192,9 @@ files:
204
192
  - lib/pry/prompt.rb
205
193
  - lib/pry/pry_class.rb
206
194
  - lib/pry/pry_instance.rb
207
- - lib/pry/rbx_path.rb
208
195
  - lib/pry/repl.rb
209
196
  - lib/pry/repl_file_loader.rb
197
+ - lib/pry/ring.rb
210
198
  - lib/pry/rubygem.rb
211
199
  - lib/pry/slop.rb
212
200
  - lib/pry/slop/LICENSE
@@ -226,7 +214,7 @@ homepage: http://pryrepl.org
226
214
  licenses:
227
215
  - MIT
228
216
  metadata: {}
229
- post_install_message:
217
+ post_install_message:
230
218
  rdoc_options: []
231
219
  require_paths:
232
220
  - lib
@@ -241,9 +229,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
241
229
  - !ruby/object:Gem::Version
242
230
  version: '0'
243
231
  requirements: []
244
- rubyforge_project:
245
- rubygems_version: 2.6.13
246
- signing_key:
232
+ rubyforge_project:
233
+ rubygems_version: 2.7.6
234
+ signing_key:
247
235
  specification_version: 4
248
236
  summary: An IRB alternative and runtime developer console
249
237
  test_files: []
@@ -1,35 +0,0 @@
1
- class Pry::Command::ListPrompts < Pry::ClassCommand
2
- match 'list-prompts'
3
- group 'Input and Output'
4
- description 'List the prompts available for use.'
5
- banner <<-BANNER
6
- Usage: list-prompts
7
-
8
- List the available prompts. You can use change-prompt to switch between
9
- them.
10
- BANNER
11
-
12
- def process
13
- output.puts heading("Available prompts") + "\n"
14
- prompt_map.each do |name, prompt|
15
- output.write "Name: #{text.bold(name)}"
16
- output.puts selected_prompt?(prompt) ? selected_text : ""
17
- output.puts prompt[:description]
18
- output.puts
19
- end
20
- end
21
-
22
- private
23
- def prompt_map
24
- Pry::Prompt::MAP
25
- end
26
-
27
- def selected_text
28
- text.red " (selected) "
29
- end
30
-
31
- def selected_prompt?(prompt)
32
- _pry_.prompt == prompt[:value]
33
- end
34
- Pry::Commands.add_command(self)
35
- end
@@ -1,22 +0,0 @@
1
- class Pry
2
- class Command::SimplePrompt < Pry::ClassCommand
3
- match 'simple-prompt'
4
- group 'prompts'
5
- description 'Toggle the simple prompt.'
6
-
7
- banner <<-'BANNER'
8
- Toggle the simple prompt.
9
- BANNER
10
-
11
- def process
12
- case _pry_.prompt
13
- when Pry::SIMPLE_PROMPT
14
- _pry_.pop_prompt
15
- else
16
- _pry_.push_prompt Pry::SIMPLE_PROMPT
17
- end
18
- end
19
- end
20
-
21
- Pry::Commands.add_command(Pry::Command::SimplePrompt)
22
- end
@@ -1,121 +0,0 @@
1
- class Pry
2
- # A history array is an array to which you can only add elements. Older
3
- # entries are removed progressively, so that the array never contains more than
4
- # N elements.
5
- #
6
- # History arrays are used by Pry to store the output of the last commands.
7
- #
8
- # @example
9
- # ary = Pry::HistoryArray.new 10
10
- # ary << 1 << 2 << 3
11
- # ary[0] # => 1
12
- # ary[1] # => 2
13
- # 10.times { |n| ary << n }
14
- # ary[0] # => nil
15
- # ary[-1] # => 9
16
- class HistoryArray
17
- include Enumerable
18
-
19
- # @param [Integer] size Maximum amount of objects in the array
20
- def initialize(size)
21
- @max_size = size
22
-
23
- @hash = {}
24
- @count = 0
25
- end
26
-
27
- # Pushes an object at the end of the array
28
- # @param [Object] value Object to be added
29
- def <<(value)
30
- @hash[@count] = value
31
-
32
- if @hash.size > max_size
33
- @hash.delete(@count - max_size)
34
- end
35
-
36
- @count += 1
37
-
38
- self
39
- end
40
-
41
- # @overload [](index)
42
- # @param [Integer] index Index of the item to access.
43
- # @return [Object, nil] Item at that index or nil if it has been removed.
44
- # @overload [](index, size)
45
- # @param [Integer] index Index of the first item to access.
46
- # @param [Integer] size Amount of items to access
47
- # @return [Array, nil] The selected items. Nil if index is greater than
48
- # the size of the array.
49
- # @overload [](range)
50
- # @param [Range<Integer>] range Range of indices to access.
51
- # @return [Array, nil] The selected items. Nil if index is greater than
52
- # the size of the array.
53
- def [](index_or_range, size = nil)
54
- if index_or_range.is_a? Integer
55
- index = convert_index(index_or_range)
56
-
57
- if size
58
- end_index = index + size
59
- index > @count ? nil : (index...[end_index, @count].min).map do |n|
60
- @hash[n]
61
- end
62
- else
63
- @hash[index]
64
- end
65
- else
66
- range = convert_range(index_or_range)
67
- range.begin > @count ? nil : range.map { |n| @hash[n] }
68
- end
69
- end
70
-
71
- # @return [Integer] Amount of objects in the array
72
- def size
73
- @count
74
- end
75
- alias count size
76
- alias length size
77
-
78
- def empty?
79
- size == 0
80
- end
81
-
82
- def each
83
- ((@count - size)...@count).each do |n|
84
- yield @hash[n]
85
- end
86
- end
87
-
88
- def to_a
89
- ((@count - size)...@count).map { |n| @hash[n] }
90
- end
91
-
92
- # @return [Hash] copy of the internal @hash history
93
- def to_h
94
- @hash.dup
95
- end
96
-
97
- def pop!
98
- @hash.delete @count - 1
99
- @count -= 1
100
- end
101
-
102
- def inspect
103
- "#<#{self.class} size=#{size} first=#{@count - size} max_size=#{max_size}>"
104
- end
105
-
106
- # @return [Integer] Maximum amount of objects in the array
107
- attr_reader :max_size
108
-
109
- private
110
- def convert_index(n)
111
- n >= 0 ? n : @count + n
112
- end
113
-
114
- def convert_range(range)
115
- end_index = convert_index(range.end)
116
- end_index += 1 unless range.exclude_end?
117
-
118
- Range.new(convert_index(range.begin), [end_index, @count].min, true)
119
- end
120
- end
121
- end