pry 0.11.1 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +184 -1
  3. data/LICENSE +1 -1
  4. data/README.md +13 -30
  5. data/bin/pry +0 -4
  6. data/lib/pry/cli.rb +17 -24
  7. data/lib/pry/code/code_file.rb +5 -4
  8. data/lib/pry/code/code_range.rb +3 -3
  9. data/lib/pry/code/loc.rb +14 -8
  10. data/lib/pry/code.rb +6 -6
  11. data/lib/pry/code_object.rb +4 -4
  12. data/lib/pry/color_printer.rb +2 -1
  13. data/lib/pry/command.rb +36 -29
  14. data/lib/pry/command_set.rb +17 -52
  15. data/lib/pry/commands/amend_line.rb +3 -4
  16. data/lib/pry/commands/bang.rb +1 -1
  17. data/lib/pry/commands/cat/exception_formatter.rb +9 -8
  18. data/lib/pry/commands/cat/input_expression_formatter.rb +1 -1
  19. data/lib/pry/commands/cat.rb +7 -6
  20. data/lib/pry/commands/change_prompt.rb +29 -9
  21. data/lib/pry/commands/clear_screen.rb +14 -0
  22. data/lib/pry/commands/code_collector.rb +9 -9
  23. data/lib/pry/commands/easter_eggs.rb +3 -3
  24. data/lib/pry/commands/edit.rb +8 -7
  25. data/lib/pry/commands/exit.rb +2 -1
  26. data/lib/pry/commands/find_method.rb +11 -13
  27. data/lib/pry/commands/gem_cd.rb +1 -1
  28. data/lib/pry/commands/gem_install.rb +2 -2
  29. data/lib/pry/commands/gem_list.rb +2 -2
  30. data/lib/pry/commands/gem_open.rb +1 -1
  31. data/lib/pry/commands/gem_search.rb +2 -2
  32. data/lib/pry/commands/gem_stats.rb +83 -0
  33. data/lib/pry/commands/gist.rb +7 -6
  34. data/lib/pry/commands/help.rb +3 -3
  35. data/lib/pry/commands/hist.rb +9 -8
  36. data/lib/pry/commands/import_set.rb +2 -1
  37. data/lib/pry/commands/install_command.rb +7 -6
  38. data/lib/pry/commands/list_inspectors.rb +2 -2
  39. data/lib/pry/commands/ls/constants.rb +4 -4
  40. data/lib/pry/commands/ls/formatter.rb +3 -2
  41. data/lib/pry/commands/ls/globals.rb +0 -2
  42. data/lib/pry/commands/ls/grep.rb +0 -2
  43. data/lib/pry/commands/ls/instance_vars.rb +0 -1
  44. data/lib/pry/commands/ls/local_names.rb +0 -2
  45. data/lib/pry/commands/ls/local_vars.rb +0 -2
  46. data/lib/pry/commands/ls/ls_entity.rb +0 -1
  47. data/lib/pry/commands/ls/methods.rb +0 -3
  48. data/lib/pry/commands/ls/methods_helper.rb +1 -1
  49. data/lib/pry/commands/ls/self_methods.rb +0 -1
  50. data/lib/pry/commands/ls.rb +27 -30
  51. data/lib/pry/commands/play.rb +1 -2
  52. data/lib/pry/commands/pry_backtrace.rb +1 -1
  53. data/lib/pry/commands/raise_up.rb +2 -1
  54. data/lib/pry/commands/ri.rb +5 -4
  55. data/lib/pry/commands/shell_command.rb +3 -2
  56. data/lib/pry/commands/shell_mode.rb +6 -6
  57. data/lib/pry/commands/show_doc.rb +5 -7
  58. data/lib/pry/commands/show_info.rb +25 -18
  59. data/lib/pry/commands/show_source.rb +5 -2
  60. data/lib/pry/commands/stat.rb +1 -1
  61. data/lib/pry/commands/watch_expression.rb +9 -7
  62. data/lib/pry/commands/whereami.rb +5 -5
  63. data/lib/pry/commands/wtf.rb +15 -2
  64. data/lib/pry/config/behavior.rb +229 -204
  65. data/lib/pry/config/convenience.rb +24 -21
  66. data/lib/pry/config/default.rb +153 -143
  67. data/lib/pry/config/memoization.rb +41 -37
  68. data/lib/pry/config.rb +33 -9
  69. data/lib/pry/core_extensions.rb +4 -3
  70. data/lib/pry/editor.rb +5 -12
  71. data/lib/pry/exceptions.rb +0 -2
  72. data/lib/pry/helpers/base_helpers.rb +197 -106
  73. data/lib/pry/helpers/command_helpers.rb +5 -4
  74. data/lib/pry/helpers/documentation_helpers.rb +2 -2
  75. data/lib/pry/helpers/options_helpers.rb +5 -5
  76. data/lib/pry/helpers/platform.rb +58 -0
  77. data/lib/pry/helpers/table.rb +20 -15
  78. data/lib/pry/helpers/text.rb +23 -9
  79. data/lib/pry/helpers.rb +1 -0
  80. data/lib/pry/history.rb +21 -8
  81. data/lib/pry/hooks.rb +3 -3
  82. data/lib/pry/indent.rb +15 -17
  83. data/lib/pry/input_completer.rb +12 -7
  84. data/lib/pry/input_lock.rb +0 -2
  85. data/lib/pry/last_exception.rb +1 -1
  86. data/lib/pry/method/disowned.rb +2 -1
  87. data/lib/pry/method/patcher.rb +2 -2
  88. data/lib/pry/method/weird_method_locator.rb +21 -11
  89. data/lib/pry/method.rb +37 -31
  90. data/lib/pry/object_path.rb +5 -4
  91. data/lib/pry/output.rb +3 -2
  92. data/lib/pry/pager.rb +4 -3
  93. data/lib/pry/platform.rb +91 -0
  94. data/lib/pry/plugins.rb +7 -3
  95. data/lib/pry/prompt.rb +144 -25
  96. data/lib/pry/pry_class.rb +53 -29
  97. data/lib/pry/pry_instance.rb +88 -55
  98. data/lib/pry/repl.rb +33 -4
  99. data/lib/pry/repl_file_loader.rb +1 -2
  100. data/lib/pry/ring.rb +84 -0
  101. data/lib/pry/rubygem.rb +6 -6
  102. data/lib/pry/slop/commands.rb +3 -4
  103. data/lib/pry/slop/option.rb +19 -21
  104. data/lib/pry/slop.rb +17 -17
  105. data/lib/pry/terminal.rb +2 -1
  106. data/lib/pry/testable/evalable.rb +15 -0
  107. data/lib/pry/testable/mockable.rb +14 -0
  108. data/lib/pry/testable/pry_tester.rb +73 -0
  109. data/lib/pry/testable/utility.rb +26 -0
  110. data/lib/pry/testable/variables.rb +46 -0
  111. data/lib/pry/testable.rb +70 -0
  112. data/lib/pry/version.rb +1 -1
  113. data/lib/pry/wrapped_module/candidate.rb +2 -2
  114. data/lib/pry/wrapped_module.rb +15 -15
  115. data/lib/pry.rb +18 -47
  116. metadata +24 -30
  117. data/lib/pry/commands/list_prompts.rb +0 -35
  118. data/lib/pry/commands/simple_prompt.rb +0 -22
  119. data/lib/pry/history_array.rb +0 -121
  120. data/lib/pry/rbx_path.rb +0 -22
  121. data/lib/pry/test/helper.rb +0 -179
@@ -21,6 +21,7 @@
21
21
  # * the IRC channel, which is #pry on the Freenode network
22
22
  #
23
23
 
24
+ # rubocop:disable Metrics/ClassLength
24
25
  class Pry
25
26
  attr_accessor :binding_stack
26
27
  attr_accessor :custom_completions
@@ -34,8 +35,13 @@ class Pry
34
35
  attr_reader :last_exception
35
36
  attr_reader :command_state
36
37
  attr_reader :exit_value
37
- attr_reader :input_array
38
- attr_reader :output_array
38
+
39
+ # @since v0.12.0
40
+ attr_reader :input_ring
41
+
42
+ # @since v0.12.0
43
+ attr_reader :output_ring
44
+
39
45
  attr_reader :config
40
46
 
41
47
  extend Pry::Config::Convenience
@@ -62,7 +68,7 @@ class Pry
62
68
  # The backtrace of the session's `binding.pry` line, if applicable.
63
69
  # @option options [Object] :target
64
70
  # The initial context for this session.
65
- def initialize(options={})
71
+ def initialize(options = {})
66
72
  @binding_stack = []
67
73
  @indent = Pry::Indent.new
68
74
  @command_state = {}
@@ -72,23 +78,47 @@ class Pry
72
78
  @config = Pry::Config.new
73
79
  config.merge!(options)
74
80
  push_prompt(config.prompt)
75
- @input_array = Pry::HistoryArray.new config.memory_size
76
- @output_array = Pry::HistoryArray.new config.memory_size
81
+ @input_ring = Pry::Ring.new(config.memory_size)
82
+ @output_ring = Pry::Ring.new(config.memory_size)
77
83
  @custom_completions = config.command_completions
78
84
  set_last_result nil
79
- @input_array << nil
85
+ @input_ring << nil
80
86
  push_initial_binding(target)
81
87
  exec_hook(:when_started, target, options, self)
82
88
  end
83
89
 
84
- # The current prompt.
90
+ @input_array_warn = false
91
+ # @deprecated Use {#input_ring} instead.
92
+ def input_array
93
+ unless @input_array_warn
94
+ loc = caller_locations(1..1).first
95
+ warn(
96
+ "#{loc.path}:#{loc.lineno}: warning: method #{self.class}##{__method__} " \
97
+ "is deprecated. Use #{self.class}#input_ring instead"
98
+ )
99
+ @input_array_warn = true
100
+ end
101
+
102
+ @input_ring
103
+ end
104
+
105
+ @output_array_warn = false
106
+ # @deprecated Use {#output_ring} instead.
107
+ def output_array
108
+ unless @output_array_warn
109
+ loc = caller_locations(1..1).first
110
+ warn(
111
+ "#{loc.path}:#{loc.lineno}: warning: method #{self.class}##{__method__} " \
112
+ "is deprecated. Use #{self.class}#output_ring instead"
113
+ )
114
+ @output_array_warn = true
115
+ end
116
+
117
+ @output_ring
118
+ end
119
+
85
120
  # This is the prompt at the top of the prompt stack.
86
- #
87
- # @example
88
- # self.prompt = Pry::SIMPLE_PROMPT
89
- # self.prompt # => Pry::SIMPLE_PROMPT
90
- #
91
- # @return [Array<Proc>] Current prompt.
121
+ # @return [Array<Proc>] the current prompt
92
122
  def prompt
93
123
  prompt_stack.last
94
124
  end
@@ -103,7 +133,7 @@ class Pry
103
133
 
104
134
  # Initialize this instance by pushing its initial context into the binding
105
135
  # stack. If no target is given, start at the top level.
106
- def push_initial_binding(target=nil)
136
+ def push_initial_binding(target = nil)
107
137
  push_binding(target || Pry.toplevel_binding)
108
138
  end
109
139
 
@@ -132,6 +162,7 @@ class Pry
132
162
  #
133
163
  def complete(str)
134
164
  return EMPTY_COMPLETIONS unless config.completer
165
+
135
166
  Pry.critical_section do
136
167
  completer = config.completer.new(config.input, self)
137
168
  completer.call str, target: current_binding, custom_completions: custom_completions.call.push(*sticky_locals.keys)
@@ -171,13 +202,13 @@ class Pry
171
202
  # @return [Integer] The maximum amount of objects remembered by the inp and
172
203
  # out arrays. Defaults to 100.
173
204
  def memory_size
174
- @output_array.max_size
205
+ @output_ring.max_size
175
206
  end
176
207
 
177
208
  undef :memory_size= if method_defined? :memory_size=
178
209
  def memory_size=(size)
179
- @input_array = Pry::HistoryArray.new(size)
180
- @output_array = Pry::HistoryArray.new(size)
210
+ @input_ring = Pry::Ring.new(size)
211
+ @output_ring = Pry::Ring.new(size)
181
212
  end
182
213
 
183
214
  # Inject all the sticky locals into the current binding.
@@ -197,14 +228,14 @@ class Pry
197
228
  end
198
229
 
199
230
  def sticky_locals
200
- { _in_: input_array,
201
- _out_: output_array,
231
+ { _in_: input_ring,
232
+ _out_: output_ring,
202
233
  _pry_: self,
203
234
  _ex_: last_exception && last_exception.wrapped_exception,
204
235
  _file_: last_file,
205
236
  _dir_: last_dir,
206
237
  _: proc { last_result },
207
- __: proc { output_array[-2] }
238
+ __: proc { output_ring[-2] }
208
239
  }.merge(config.extra_sticky_locals)
209
240
  end
210
241
 
@@ -234,7 +265,7 @@ class Pry
234
265
  # @return [Boolean] Is Pry ready to accept more input?
235
266
  # @raise [Exception] If the user uses the `raise-up` command, this method
236
267
  # will raise that exception.
237
- def eval(line, options={})
268
+ def eval(line, options = {})
238
269
  return false if @stopped
239
270
 
240
271
  exit_value = nil
@@ -254,6 +285,7 @@ class Pry
254
285
 
255
286
  # TODO: make this configurable?
256
287
  raise exception if exception
288
+
257
289
  return false
258
290
  end
259
291
 
@@ -310,7 +342,7 @@ class Pry
310
342
  # This workaround has a side effect: java exceptions specified
311
343
  # in `Pry.config.exception_whitelist` are ignored.
312
344
  jruby_exceptions = []
313
- if Pry::Helpers::BaseHelpers.jruby?
345
+ if Helpers::Platform.jruby?
314
346
  jruby_exceptions << Java::JavaLang::Exception
315
347
  end
316
348
 
@@ -325,7 +357,7 @@ class Pry
325
357
  # Eliminate following warning:
326
358
  # warning: singleton on non-persistent Java type X
327
359
  # (http://wiki.jruby.org/Persistence)
328
- if Pry::Helpers::BaseHelpers.jruby? && e.class.respond_to?('__persistent__')
360
+ if Helpers::Platform.jruby? && e.class.respond_to?('__persistent__')
329
361
  e.class.__persistent__ = true
330
362
  end
331
363
  self.last_exception = e
@@ -345,7 +377,7 @@ class Pry
345
377
  # (If nested sessions are going to exist, this method is fine, but a goal is
346
378
  # to come up with an alternative to nested sessions altogether.)
347
379
  def repl(target = nil)
348
- Pry::REPL.new(self, :target => target).start
380
+ Pry::REPL.new(self, target: target).start
349
381
  end
350
382
 
351
383
  def evaluate_ruby(code)
@@ -403,11 +435,11 @@ class Pry
403
435
  val = val.lstrip if /^\s\S/ !~ val
404
436
  val = val.chomp
405
437
  result = commands.process_line(val,
406
- :target => current_binding,
407
- :output => output,
408
- :eval_string => @eval_string,
409
- :pry_instance => self,
410
- :hooks => hooks
438
+ target: current_binding,
439
+ output: output,
440
+ eval_string: @eval_string,
441
+ pry_instance: self,
442
+ hooks: hooks
411
443
  )
412
444
 
413
445
  # set a temporary (just so we can inject the value we want into eval_string)
@@ -448,10 +480,10 @@ class Pry
448
480
  # pry_instance.run_command("ls -m")
449
481
  def run_command(val)
450
482
  commands.process_line(val,
451
- :eval_string => @eval_string,
452
- :target => current_binding,
453
- :pry_instance => self,
454
- :output => output
483
+ eval_string: @eval_string,
484
+ target: current_binding,
485
+ pry_instance: self,
486
+ output: output
455
487
  )
456
488
  Pry::Command::VOID_VALUE
457
489
  end
@@ -479,9 +511,9 @@ class Pry
479
511
  # This method should not need to be invoked directly.
480
512
  # @param [Object] result The result.
481
513
  # @param [String] code The code that was run.
482
- def set_last_result(result, code="")
514
+ def set_last_result(result, code = "")
483
515
  @last_result_is_exception = false
484
- @output_array << result
516
+ @output_ring << result
485
517
 
486
518
  self.last_result = result unless code =~ /\A\s*\z/
487
519
  end
@@ -495,7 +527,7 @@ class Pry
495
527
  def last_exception=(e)
496
528
  last_exception = Pry::LastException.new(e)
497
529
  @last_result_is_exception = true
498
- @output_array << last_exception
530
+ @output_ring << last_exception
499
531
  @last_exception = last_exception
500
532
  end
501
533
 
@@ -503,8 +535,8 @@ class Pry
503
535
  # This method should not need to be invoked directly.
504
536
  # @param [String] code The code we just eval'd
505
537
  def update_input_history(code)
506
- # Always push to the @input_array as the @output_array is always pushed to.
507
- @input_array << code
538
+ # Always push to the @input_ring as the @output_ring is always pushed to.
539
+ @input_ring << code
508
540
  if code
509
541
  Pry.line_buffer.push(*code.each_line)
510
542
  Pry.current_line += code.lines.count
@@ -529,28 +561,26 @@ class Pry
529
561
  # @return [String] The prompt.
530
562
  def select_prompt
531
563
  object = current_binding.eval('self')
532
-
533
- open_token = @indent.open_delimiters.any? ? @indent.open_delimiters.last :
534
- @indent.stack.last
535
-
536
- c = Pry::Config.from_hash({
537
- :object => object,
538
- :nesting_level => binding_stack.size - 1,
539
- :open_token => open_token,
540
- :session_line => Pry.history.session_line_count + 1,
541
- :history_line => Pry.history.history_line_count + 1,
542
- :expr_number => input_array.count,
543
- :_pry_ => self,
544
- :binding_stack => binding_stack,
545
- :input_array => input_array,
546
- :eval_string => @eval_string,
547
- :cont => !@eval_string.empty?})
564
+ open_token = @indent.open_delimiters.last || @indent.stack.last
565
+
566
+ c = Pry::Config.assign({
567
+ object: object,
568
+ nesting_level: binding_stack.size - 1,
569
+ open_token: open_token,
570
+ session_line: Pry.history.session_line_count + 1,
571
+ history_line: Pry.history.history_line_count + 1,
572
+ expr_number: input_ring.count,
573
+ _pry_: self,
574
+ binding_stack: binding_stack,
575
+ input_ring: input_ring,
576
+ eval_string: @eval_string,
577
+ cont: !@eval_string.empty?
578
+ })
548
579
 
549
580
  Pry.critical_section do
550
581
  # If input buffer is empty then use normal prompt
551
582
  if eval_string.empty?
552
583
  generate_prompt(Array(prompt).first, c)
553
-
554
584
  # Otherwise use the wait prompt (indicating multi-line expression)
555
585
  else
556
586
  generate_prompt(Array(prompt).last, c)
@@ -655,7 +685,9 @@ class Pry
655
685
  raise exception
656
686
  end
657
687
  end
688
+
658
689
  def raise_up(*args); raise_up_common(false, *args); end
690
+
659
691
  def raise_up!(*args); raise_up_common(true, *args); end
660
692
 
661
693
  # Convenience accessor for the `quiet` config key.
@@ -664,3 +696,4 @@ class Pry
664
696
  config.quiet
665
697
  end
666
698
  end
699
+ # rubocop:enable Metrics/ClassLength
data/lib/pry/repl.rb CHANGED
@@ -49,7 +49,7 @@ class Pry
49
49
 
50
50
  # Clear the line before starting Pry. This fixes issue #566.
51
51
  if pry.config.correct_indent
52
- Kernel.print Pry::Helpers::BaseHelpers.windows_ansi? ? "\e[0F" : "\e[0G"
52
+ Kernel.print(Helpers::Platform.windows_ansi? ? "\e[0F" : "\e[0G")
53
53
  end
54
54
  end
55
55
 
@@ -106,8 +106,9 @@ class Pry
106
106
 
107
107
  if output.tty? && pry.config.correct_indent && Pry::Helpers::BaseHelpers.use_ansi_codes?
108
108
  output.print @indent.correct_indentation(
109
- current_prompt, indented_val,
110
- original_val.length - indented_val.length
109
+ current_prompt,
110
+ indented_val,
111
+ calculate_overhang(current_prompt, original_val, indented_val)
111
112
  )
112
113
  output.flush
113
114
  end
@@ -218,15 +219,43 @@ class Pry
218
219
  # % pry | tee log
219
220
  def piping?
220
221
  return false unless $stdout.respond_to?(:tty?)
221
- !$stdout.tty? && $stdin.tty? && !Pry::Helpers::BaseHelpers.windows?
222
+
223
+ !$stdout.tty? && $stdin.tty? && !Helpers::Platform.windows?
222
224
  end
223
225
 
224
226
  # @return [void]
225
227
  def set_readline_output
226
228
  return if @readline_output
229
+
227
230
  if piping?
228
231
  @readline_output = (Readline.output = Pry.config.output)
229
232
  end
230
233
  end
234
+
235
+ # Calculates correct overhang for current line. Supports vi Readline
236
+ # mode and its indicators such as "(ins)" or "(cmd)".
237
+ #
238
+ # @return [Integer]
239
+ # @note This doesn't calculate overhang for Readline's emacs mode with an
240
+ # indicator because emacs is the default mode and it doesn't use
241
+ # indicators in 99% of cases.
242
+ def calculate_overhang(current_prompt, original_val, indented_val)
243
+ overhang = original_val.length - indented_val.length
244
+
245
+ if readline_available? && Readline.respond_to?(:vi_editing_mode?)
246
+ begin
247
+ # rb-readline doesn't support this method:
248
+ # https://github.com/ConnorAtherton/rb-readline/issues/152
249
+ if Readline.vi_editing_mode?
250
+ overhang += current_prompt.length - indented_val.length
251
+ end
252
+ rescue NotImplementedError
253
+ # VI editing mode is unsupported on JRuby.
254
+ # https://github.com/pry/pry/issues/1840
255
+ nil
256
+ end
257
+ end
258
+ [0, overhang].max
259
+ end
231
260
  end
232
261
  end
@@ -1,5 +1,4 @@
1
1
  class Pry
2
-
3
2
  # A class to manage the loading of files through the REPL loop.
4
3
  # This is an interesting trick as it processes your file as if it
5
4
  # was user input in an interactive session. As a result, all Pry
@@ -42,7 +41,7 @@ class Pry
42
41
  end
43
42
 
44
43
  content.lines.each do |line|
45
- break unless _pry_.eval line, :generated => true
44
+ break unless _pry_.eval line, generated: true
46
45
  end
47
46
 
48
47
  unless _pry_.eval_string.empty?
data/lib/pry/ring.rb ADDED
@@ -0,0 +1,84 @@
1
+ class Pry
2
+ # A ring is a thread-safe fixed-capacity array to which you can only add
3
+ # elements. Older entries are overwritten as you add new elements, so that the
4
+ # ring can never contain more than `max_size` elemens.
5
+ #
6
+ # @example
7
+ # ring = Pry::Ring.new(3)
8
+ # ring << 1 << 2 << 3
9
+ # ring.to_a #=> [1, 2, 3]
10
+ # ring << 4
11
+ # ring.to_a #=> [2, 3, 4]
12
+ #
13
+ # ring[0] #=> 2
14
+ # ring[-1] #=> 4
15
+ # ring.clear
16
+ # ring[0] #=> nil
17
+ #
18
+ # @api public
19
+ # @since v0.12.0
20
+ class Ring
21
+ # @return [Integer] maximum buffer size
22
+ attr_reader :max_size
23
+
24
+ # @return [Integer] how many objects were added during the lifetime of the
25
+ # ring
26
+ attr_reader :count
27
+ alias size count
28
+
29
+ # @param [Integer] max_size Maximum buffer size. The buffer will start
30
+ # overwriting elements once its reaches its maximum capacity
31
+ def initialize(max_size)
32
+ @max_size = max_size
33
+ @mutex = Mutex.new
34
+ clear
35
+ end
36
+
37
+ # Push `value` to the current index.
38
+ #
39
+ # @param [Object] value
40
+ # @return [self]
41
+ def <<(value)
42
+ @mutex.synchronize do
43
+ @buffer[count % max_size] = value
44
+ @count += 1
45
+ self
46
+ end
47
+ end
48
+
49
+ # Read the value stored at `index`.
50
+ #
51
+ # @param [Integer, Range] index The element (if Integer) or elements
52
+ # (if Range) associated with `index`
53
+ # @return [Object, Array<Object>, nil] element(s) at `index`, `nil` if none
54
+ # exist
55
+ def [](index)
56
+ @mutex.synchronize do
57
+ return @buffer[(count + index) % max_size] if index.is_a?(Integer)
58
+ return @buffer[index] if count <= max_size
59
+
60
+ # Swap parts of array when the array turns page and starts overwriting
61
+ # from the beginning, then apply the range.
62
+ last_part = @buffer.slice([index.end, max_size - 1].min, count % max_size)
63
+ (last_part + (@buffer - last_part))[index]
64
+ end
65
+ end
66
+
67
+ # @return [Array<Object>] the buffer as unwinded array
68
+ def to_a
69
+ return @buffer.dup if count <= max_size
70
+
71
+ last_part = @buffer.slice(count % max_size, @buffer.size)
72
+ last_part + (@buffer - last_part)
73
+ end
74
+
75
+ # Clear the buffer and reset count.
76
+ # @return [void]
77
+ def clear
78
+ @mutex.synchronize do
79
+ @buffer = []
80
+ @count = 0
81
+ end
82
+ end
83
+ end
84
+ end
data/lib/pry/rubygem.rb CHANGED
@@ -23,7 +23,7 @@ class Pry
23
23
  Gem.source_index.find_name(name)
24
24
  end
25
25
 
26
- first_spec = specs.sort_by{ |spec| Gem::Version.new(spec.version) }.last
26
+ first_spec = specs.sort_by { |spec| Gem::Version.new(spec.version) }.last
27
27
 
28
28
  first_spec or raise CommandError, "Gem `#{name}` not found"
29
29
  end
@@ -34,9 +34,9 @@ class Pry
34
34
  # @return [Array<Gem::Specification>]
35
35
  def list(pattern = /.*/)
36
36
  if Gem::Specification.respond_to?(:each)
37
- Gem::Specification.select{|spec| spec.name =~ pattern }
37
+ Gem::Specification.select { |spec| spec.name =~ pattern }
38
38
  else
39
- Gem.source_index.gems.values.select{|spec| spec.name =~ pattern }
39
+ Gem.source_index.gems.values.select { |spec| spec.name =~ pattern }
40
40
  end
41
41
  end
42
42
 
@@ -67,14 +67,14 @@ class Pry
67
67
  else
68
68
  Gem.user_dir
69
69
  end
70
- installer = Gem::DependencyInstaller.new(:install_dir => destination)
70
+ installer = Gem::DependencyInstaller.new(install_dir: destination)
71
71
  installer.install(name)
72
72
  rescue Errno::EACCES
73
73
  raise CommandError,
74
- "Insufficient permissions to install #{ Pry::Helpers::Text.green(name) }."
74
+ "Insufficient permissions to install #{green(name)}."
75
75
  rescue Gem::GemNotFoundException
76
76
  raise CommandError,
77
- "Gem #{ Pry::Helpers::Text.green(name) } not found. Aborting installation."
77
+ "Gem #{green(name)} not found. Aborting installation."
78
78
  else
79
79
  Gem.refresh
80
80
  end
@@ -133,13 +133,13 @@ class Pry::Slop
133
133
  #
134
134
  # Returns the original Array of items with options removed.
135
135
  def parse!(items = ARGV)
136
- if opts = commands[items[0].to_s]
136
+ if (opts = commands[items[0].to_s])
137
137
  @triggered_command = items.shift
138
138
  execute_arguments! items
139
139
  opts.parse! items
140
140
  execute_global_opts! items
141
141
  else
142
- if opts = commands['default']
142
+ if (opts = commands['default'])
143
143
  opts.parse! items
144
144
  else
145
145
  if config[:strict] && items[0]
@@ -187,10 +187,9 @@ class Pry::Slop
187
187
 
188
188
  # Returns nothing.
189
189
  def execute_global_opts!(items)
190
- if global_opts = commands['global']
190
+ if (global_opts = commands['global'])
191
191
  global_opts.parse! items
192
192
  end
193
193
  end
194
-
195
194
  end
196
195
  end
@@ -1,20 +1,19 @@
1
1
  class Pry::Slop
2
2
  class Option
3
-
4
3
  # The default Hash of configuration options this class uses.
5
4
  DEFAULT_OPTIONS = {
6
- :argument => false,
7
- :optional_argument => false,
8
- :tail => false,
9
- :default => nil,
10
- :callback => nil,
11
- :delimiter => ',',
12
- :limit => 0,
13
- :match => nil,
14
- :optional => true,
15
- :required => false,
16
- :as => String,
17
- :autocreated => false
5
+ argument: false,
6
+ optional_argument: false,
7
+ tail: false,
8
+ default: nil,
9
+ callback: nil,
10
+ delimiter: ',',
11
+ limit: 0,
12
+ match: nil,
13
+ optional: true,
14
+ required: false,
15
+ as: String,
16
+ autocreated: false
18
17
  }
19
18
 
20
19
  attr_reader :short, :long, :description, :config, :types
@@ -41,12 +40,12 @@ class Pry::Slop
41
40
  @value = nil
42
41
 
43
42
  @types = {
44
- :string => proc { |v| v.to_s },
45
- :symbol => proc { |v| v.to_sym },
46
- :integer => proc { |v| value_to_integer(v) },
47
- :float => proc { |v| value_to_float(v) },
48
- :range => proc { |v| value_to_range(v) },
49
- :count => proc { |v| @count }
43
+ string: proc { |v| v.to_s },
44
+ symbol: proc { |v| v.to_sym },
45
+ integer: proc { |v| value_to_integer(v) },
46
+ float: proc { |v| value_to_float(v) },
47
+ range: proc { |v| value_to_range(v) },
48
+ count: proc { |v| @count }
50
49
  }
51
50
 
52
51
  if long && long.size > @slop.config[:longest_flag]
@@ -114,7 +113,7 @@ class Pry::Slop
114
113
  if type.respond_to?(:call)
115
114
  type.call(value)
116
115
  else
117
- if callable = types[type.to_s.downcase.to_sym]
116
+ if (callable = types[type.to_s.downcase.to_sym])
118
117
  callable.call(value)
119
118
  else
120
119
  value
@@ -203,6 +202,5 @@ class Pry::Slop
203
202
  end
204
203
  end
205
204
  end
206
-
207
205
  end
208
206
  end