pry 0.12.2 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +110 -1
  3. data/LICENSE +1 -1
  4. data/README.md +331 -269
  5. data/bin/pry +5 -0
  6. data/lib/pry.rb +133 -119
  7. data/lib/pry/basic_object.rb +8 -4
  8. data/lib/pry/block_command.rb +22 -0
  9. data/lib/pry/class_command.rb +194 -0
  10. data/lib/pry/cli.rb +40 -31
  11. data/lib/pry/code.rb +39 -27
  12. data/lib/pry/code/code_file.rb +28 -24
  13. data/lib/pry/code/code_range.rb +4 -2
  14. data/lib/pry/code/loc.rb +15 -8
  15. data/lib/pry/code_object.rb +40 -38
  16. data/lib/pry/color_printer.rb +47 -46
  17. data/lib/pry/command.rb +166 -369
  18. data/lib/pry/command_set.rb +76 -73
  19. data/lib/pry/command_state.rb +31 -0
  20. data/lib/pry/commands/amend_line.rb +86 -81
  21. data/lib/pry/commands/bang.rb +18 -14
  22. data/lib/pry/commands/bang_pry.rb +15 -11
  23. data/lib/pry/commands/cat.rb +61 -54
  24. data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
  25. data/lib/pry/commands/cat/exception_formatter.rb +71 -60
  26. data/lib/pry/commands/cat/file_formatter.rb +55 -49
  27. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  28. data/lib/pry/commands/cd.rb +40 -35
  29. data/lib/pry/commands/change_inspector.rb +29 -22
  30. data/lib/pry/commands/change_prompt.rb +44 -39
  31. data/lib/pry/commands/clear_screen.rb +16 -10
  32. data/lib/pry/commands/code_collector.rb +148 -133
  33. data/lib/pry/commands/disable_pry.rb +23 -19
  34. data/lib/pry/commands/easter_eggs.rb +19 -30
  35. data/lib/pry/commands/edit.rb +184 -161
  36. data/lib/pry/commands/edit/exception_patcher.rb +21 -17
  37. data/lib/pry/commands/edit/file_and_line_locator.rb +34 -23
  38. data/lib/pry/commands/exit.rb +39 -35
  39. data/lib/pry/commands/exit_all.rb +24 -20
  40. data/lib/pry/commands/exit_program.rb +20 -16
  41. data/lib/pry/commands/find_method.rb +168 -160
  42. data/lib/pry/commands/fix_indent.rb +16 -12
  43. data/lib/pry/commands/help.rb +140 -133
  44. data/lib/pry/commands/hist.rb +151 -150
  45. data/lib/pry/commands/import_set.rb +20 -16
  46. data/lib/pry/commands/jump_to.rb +25 -21
  47. data/lib/pry/commands/list_inspectors.rb +35 -28
  48. data/lib/pry/commands/ls.rb +124 -102
  49. data/lib/pry/commands/ls/constants.rb +59 -42
  50. data/lib/pry/commands/ls/formatter.rb +50 -46
  51. data/lib/pry/commands/ls/globals.rb +38 -34
  52. data/lib/pry/commands/ls/grep.rb +17 -13
  53. data/lib/pry/commands/ls/instance_vars.rb +29 -27
  54. data/lib/pry/commands/ls/interrogatable.rb +18 -12
  55. data/lib/pry/commands/ls/jruby_hacks.rb +47 -41
  56. data/lib/pry/commands/ls/local_names.rb +26 -22
  57. data/lib/pry/commands/ls/local_vars.rb +38 -28
  58. data/lib/pry/commands/ls/ls_entity.rb +47 -51
  59. data/lib/pry/commands/ls/methods.rb +44 -43
  60. data/lib/pry/commands/ls/methods_helper.rb +46 -42
  61. data/lib/pry/commands/ls/self_methods.rb +23 -22
  62. data/lib/pry/commands/nesting.rb +21 -17
  63. data/lib/pry/commands/play.rb +93 -82
  64. data/lib/pry/commands/pry_backtrace.rb +24 -17
  65. data/lib/pry/commands/pry_version.rb +15 -11
  66. data/lib/pry/commands/raise_up.rb +27 -22
  67. data/lib/pry/commands/reload_code.rb +60 -48
  68. data/lib/pry/commands/reset.rb +16 -12
  69. data/lib/pry/commands/ri.rb +55 -45
  70. data/lib/pry/commands/save_file.rb +45 -43
  71. data/lib/pry/commands/shell_command.rb +51 -51
  72. data/lib/pry/commands/shell_mode.rb +21 -17
  73. data/lib/pry/commands/show_doc.rb +81 -68
  74. data/lib/pry/commands/show_info.rb +189 -171
  75. data/lib/pry/commands/show_input.rb +16 -11
  76. data/lib/pry/commands/show_source.rb +109 -45
  77. data/lib/pry/commands/stat.rb +35 -31
  78. data/lib/pry/commands/switch_to.rb +21 -15
  79. data/lib/pry/commands/toggle_color.rb +20 -16
  80. data/lib/pry/commands/watch_expression.rb +89 -86
  81. data/lib/pry/commands/watch_expression/expression.rb +32 -27
  82. data/lib/pry/commands/whereami.rb +156 -148
  83. data/lib/pry/commands/wtf.rb +75 -50
  84. data/lib/pry/config.rb +311 -25
  85. data/lib/pry/config/attributable.rb +22 -0
  86. data/lib/pry/config/lazy_value.rb +29 -0
  87. data/lib/pry/config/memoized_value.rb +34 -0
  88. data/lib/pry/config/value.rb +24 -0
  89. data/lib/pry/control_d_handler.rb +28 -0
  90. data/lib/pry/core_extensions.rb +9 -7
  91. data/lib/pry/editor.rb +48 -21
  92. data/lib/pry/env.rb +18 -0
  93. data/lib/pry/exception_handler.rb +43 -0
  94. data/lib/pry/exceptions.rb +13 -16
  95. data/lib/pry/forwardable.rb +5 -1
  96. data/lib/pry/helpers.rb +2 -0
  97. data/lib/pry/helpers/base_helpers.rb +68 -197
  98. data/lib/pry/helpers/command_helpers.rb +50 -61
  99. data/lib/pry/helpers/documentation_helpers.rb +20 -13
  100. data/lib/pry/helpers/options_helpers.rb +14 -7
  101. data/lib/pry/helpers/platform.rb +7 -5
  102. data/lib/pry/helpers/table.rb +33 -26
  103. data/lib/pry/helpers/text.rb +17 -14
  104. data/lib/pry/history.rb +48 -56
  105. data/lib/pry/hooks.rb +21 -12
  106. data/lib/pry/indent.rb +54 -50
  107. data/lib/pry/input_completer.rb +248 -230
  108. data/lib/pry/input_lock.rb +8 -9
  109. data/lib/pry/inspector.rb +36 -24
  110. data/lib/pry/last_exception.rb +45 -45
  111. data/lib/pry/method.rb +141 -94
  112. data/lib/pry/method/disowned.rb +16 -4
  113. data/lib/pry/method/patcher.rb +12 -3
  114. data/lib/pry/method/weird_method_locator.rb +68 -44
  115. data/lib/pry/object_path.rb +33 -25
  116. data/lib/pry/output.rb +121 -35
  117. data/lib/pry/pager.rb +41 -42
  118. data/lib/pry/plugins.rb +25 -8
  119. data/lib/pry/prompt.rb +123 -54
  120. data/lib/pry/pry_class.rb +61 -98
  121. data/lib/pry/pry_instance.rb +217 -215
  122. data/lib/pry/repl.rb +18 -22
  123. data/lib/pry/repl_file_loader.rb +27 -21
  124. data/lib/pry/ring.rb +11 -6
  125. data/lib/pry/slop.rb +574 -563
  126. data/lib/pry/slop/commands.rb +164 -169
  127. data/lib/pry/slop/option.rb +172 -168
  128. data/lib/pry/syntax_highlighter.rb +26 -0
  129. data/lib/pry/system_command_handler.rb +17 -0
  130. data/lib/pry/testable.rb +59 -61
  131. data/lib/pry/testable/evalable.rb +21 -12
  132. data/lib/pry/testable/mockable.rb +18 -10
  133. data/lib/pry/testable/pry_tester.rb +71 -56
  134. data/lib/pry/testable/utility.rb +29 -21
  135. data/lib/pry/testable/variables.rb +49 -43
  136. data/lib/pry/version.rb +3 -1
  137. data/lib/pry/warning.rb +27 -0
  138. data/lib/pry/wrapped_module.rb +51 -42
  139. data/lib/pry/wrapped_module/candidate.rb +21 -14
  140. metadata +31 -30
  141. data/lib/pry/commands.rb +0 -6
  142. data/lib/pry/commands/disabled_commands.rb +0 -2
  143. data/lib/pry/commands/gem_cd.rb +0 -26
  144. data/lib/pry/commands/gem_install.rb +0 -32
  145. data/lib/pry/commands/gem_list.rb +0 -33
  146. data/lib/pry/commands/gem_open.rb +0 -29
  147. data/lib/pry/commands/gem_readme.rb +0 -25
  148. data/lib/pry/commands/gem_search.rb +0 -40
  149. data/lib/pry/commands/gem_stats.rb +0 -83
  150. data/lib/pry/commands/gist.rb +0 -102
  151. data/lib/pry/commands/install_command.rb +0 -54
  152. data/lib/pry/config/behavior.rb +0 -255
  153. data/lib/pry/config/convenience.rb +0 -28
  154. data/lib/pry/config/default.rb +0 -159
  155. data/lib/pry/config/memoization.rb +0 -48
  156. data/lib/pry/platform.rb +0 -91
  157. data/lib/pry/rubygem.rb +0 -84
  158. data/lib/pry/terminal.rb +0 -91
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'coderay'
4
+
5
+ class Pry
6
+ # @api private
7
+ # @since v0.13.0
8
+ class SyntaxHighlighter
9
+ def self.highlight(code, language = :ruby)
10
+ tokenize(code, language).term
11
+ end
12
+
13
+ def self.tokenize(code, language = :ruby)
14
+ CodeRay.scan(code, language)
15
+ end
16
+
17
+ def self.keyword_token_color
18
+ CodeRay::Encoders::Terminal::TOKEN_COLORS[:keyword]
19
+ end
20
+
21
+ # Sets comment token to blue (black by default), so it's more legible.
22
+ def self.overwrite_coderay_comment_token!
23
+ CodeRay::Encoders::Terminal::TOKEN_COLORS[:comment][:self] = "\e[1;34m"
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Pry
4
+ # @api private
5
+ # @since v0.13.0
6
+ module SystemCommandHandler
7
+ class << self
8
+ def default(output, command, _pry_instance)
9
+ return if Kernel.system(command)
10
+
11
+ output.puts(
12
+ "Error: there was a problem executing system command: #{command}"
13
+ )
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,70 +1,68 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # good idea ???
2
4
  # if you're testing pry plugin you should require pry by yourself, no?
3
- require 'pry' if not defined?(Pry)
5
+ require 'pry' unless defined?(Pry)
4
6
 
5
- module Pry::Testable
6
- extend self
7
- require_relative "testable/pry_tester"
8
- require_relative "testable/evalable"
9
- require_relative "testable/mockable"
10
- require_relative "testable/variables"
11
- require_relative "testable/utility"
7
+ class Pry
8
+ module Testable
9
+ require_relative "testable/pry_tester"
10
+ require_relative "testable/evalable"
11
+ require_relative "testable/mockable"
12
+ require_relative "testable/variables"
13
+ require_relative "testable/utility"
12
14
 
13
- #
14
- # When {Pry::Testable} is included into another module or class,
15
- # the following modules are also included: {Pry::Testable::Mockable},
16
- # {Pry::Testable::Evalable}, {Pry::Testable::Variables}, and
17
- # {Pry::Testable::Utility}.
18
- #
19
- # @note
20
- # Each of the included modules mentioned above may also be used
21
- # standalone or in a pick-and-mix fashion.
22
- #
23
- # @param [Module] mod
24
- # A class or module.
25
- #
26
- # @return [void]
27
- #
28
- def self.included(mod)
29
- mod.module_eval do
30
- include Pry::Testable::Mockable
31
- include Pry::Testable::Evalable
32
- include Pry::Testable::Variables
33
- include Pry::Testable::Utility
15
+ #
16
+ # When {Pry::Testable} is included into another module or class,
17
+ # the following modules are also included: {Pry::Testable::Mockable},
18
+ # {Pry::Testable::Evalable}, {Pry::Testable::Variables}, and
19
+ # {Pry::Testable::Utility}.
20
+ #
21
+ # @note
22
+ # Each of the included modules mentioned above may also be used
23
+ # standalone or in a pick-and-mix fashion.
24
+ #
25
+ # @param [Module] mod
26
+ # A class or module.
27
+ #
28
+ # @return [void]
29
+ #
30
+ def self.included(mod)
31
+ mod.module_eval do
32
+ include Pry::Testable::Mockable
33
+ include Pry::Testable::Evalable
34
+ include Pry::Testable::Variables
35
+ include Pry::Testable::Utility
36
+ end
34
37
  end
35
- end
36
-
37
- TEST_DEFAULTS = {
38
- color: false,
39
- pager: false,
40
- should_load_rc: false,
41
- should_load_local_rc: false,
42
- correct_indent: false,
43
- collison_warning: false,
44
- history: {
45
- should_load: false,
46
- should_save: false
47
- }
48
- }
49
- private_constant :TEST_DEFAULTS
50
38
 
51
- #
52
- # Sets various configuration options that make Pry optimal for a test
53
- # environment, see source code for complete details.
54
- #
55
- # @return [void]
56
- #
57
- def self.set_testenv_variables
58
- Pry.config = Pry::Config.from_hash(TEST_DEFAULTS, Pry::Config::Default.new)
59
- Pry.config.hooks = Pry::Hooks.new
60
- end
39
+ #
40
+ # Sets various configuration options that make Pry optimal for a test
41
+ # environment, see source code for complete details.
42
+ #
43
+ # @return [void]
44
+ #
45
+ def self.set_testenv_variables
46
+ Pry.config = Pry::Config.new.merge(
47
+ color: false,
48
+ pager: false,
49
+ should_load_rc: false,
50
+ should_load_local_rc: false,
51
+ correct_indent: false,
52
+ collision_warning: false,
53
+ history_save: false,
54
+ history_load: false,
55
+ hooks: Pry::Hooks.new
56
+ )
57
+ end
61
58
 
62
- #
63
- # Reset the Pry configuration to their default values.
64
- #
65
- # @return [void]
66
- #
67
- def self.unset_testenv_variables
68
- Pry.config = Pry::Config.from_hash({}, Pry::Config::Default.new)
59
+ #
60
+ # Reset the Pry configuration to their default values.
61
+ #
62
+ # @return [void]
63
+ #
64
+ def self.unset_testenv_variables
65
+ Pry.config = Pry::Config.new
66
+ end
69
67
  end
70
68
  end
@@ -1,15 +1,24 @@
1
- module Pry::Testable::Evalable
2
- def pry_tester(*args, &block)
3
- if args.length == 0 || args[0].is_a?(Hash)
4
- args.unshift(Pry.toplevel_binding)
5
- end
6
- Pry::Testable::PryTester.new(*args).tap do |t|
7
- t.singleton_class.class_eval(&block) if block
8
- end
9
- end
1
+ # frozen_string_literal: true
10
2
 
11
- def pry_eval(*eval_strs)
12
- b = String === eval_strs.first ? Pry.toplevel_binding : Pry.binding_for(eval_strs.shift)
13
- pry_tester(b).eval(*eval_strs)
3
+ class Pry
4
+ module Testable
5
+ module Evalable
6
+ def pry_tester(*args, &block)
7
+ args.unshift(Pry.toplevel_binding) if args.empty? || args[0].is_a?(Hash)
8
+ Pry::Testable::PryTester.new(*args).tap do |t|
9
+ t.singleton_class.class_eval(&block) if block
10
+ end
11
+ end
12
+
13
+ def pry_eval(*eval_strs)
14
+ b =
15
+ if eval_strs.first.is_a?(String)
16
+ Pry.toplevel_binding
17
+ else
18
+ Pry.binding_for(eval_strs.shift)
19
+ end
20
+ pry_tester(b).eval(*eval_strs)
21
+ end
22
+ end
14
23
  end
15
24
  end
@@ -1,14 +1,22 @@
1
- module Pry::Testable::Mockable
2
- def mock_command(cmd, args = [], opts = {})
3
- output = StringIO.new
4
- pry = Pry.new(output: output)
5
- ret = cmd.new(opts.merge(pry_instance: pry, output: output)).call_safely(*args)
6
- Struct.new(:output, :return).new(output.string, ret)
7
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'stringio'
4
+
5
+ class Pry
6
+ module Testable
7
+ module Mockable
8
+ def mock_command(cmd, args = [], opts = {})
9
+ output = StringIO.new
10
+ pry = Pry.new(output: output)
11
+ ret = cmd.new(opts.merge(pry_instance: pry, output: output)).call_safely(*args)
12
+ Struct.new(:output, :return).new(output.string, ret)
13
+ end
8
14
 
9
- def mock_exception(*mock_backtrace)
10
- StandardError.new.tap do |e|
11
- e.define_singleton_method(:backtrace) { mock_backtrace }
15
+ def mock_exception(*mock_backtrace)
16
+ StandardError.new.tap do |e|
17
+ e.define_singleton_method(:backtrace) { mock_backtrace }
18
+ end
19
+ end
12
20
  end
13
21
  end
14
22
  end
@@ -1,73 +1,88 @@
1
- class Pry::Testable::PryTester
2
- extend Pry::Forwardable
3
- attr_reader :pry, :out
4
- def_delegators :@pry, :eval_string, :eval_string=
1
+ # frozen_string_literal: true
5
2
 
6
- def initialize(target = TOPLEVEL_BINDING, options = {})
7
- @pry = Pry.new(options.merge(target: target))
8
- @history = options[:history]
9
- @pry.inject_sticky_locals!
10
- reset_output
11
- end
3
+ require 'stringio'
12
4
 
13
- def eval(*strs)
14
- reset_output
15
- result = nil
5
+ class Pry
6
+ module Testable
7
+ class PryTester
8
+ extend Pry::Forwardable
9
+ attr_reader :pry, :out
10
+ def_delegators :@pry, :eval_string, :eval_string=
16
11
 
17
- strs.flatten.each do |str|
18
- # Check for space prefix. See #1369.
19
- if str !~ /^\s\S/
20
- str = "#{str.strip}\n"
12
+ def initialize(target = TOPLEVEL_BINDING, options = {})
13
+ @pry = Pry.new(options.merge(target: target))
14
+ @history = options[:history]
15
+ @pry.inject_sticky_locals!
16
+ reset_output
21
17
  end
22
- @history.push str if @history
23
18
 
24
- if @pry.process_command(str)
25
- result = last_command_result_or_output
26
- else
27
- result = @pry.evaluate_ruby(str)
19
+ def eval(*strs)
20
+ reset_output
21
+ result = nil
22
+
23
+ strs.flatten.each do |str|
24
+ # Check for space prefix. See #1369.
25
+ str = "#{str.strip}\n" if str !~ /^\s\S/
26
+ @history.push str if @history
27
+
28
+ result =
29
+ if @pry.process_command(str)
30
+ last_command_result_or_output
31
+ else
32
+ # Check if this is a multiline paste.
33
+ begin
34
+ complete_expr = Pry::Code.complete_expression?(str)
35
+ rescue SyntaxError => exception
36
+ @pry.output.puts(
37
+ "SyntaxError: #{exception.message.sub(/.*syntax error, */m, '')}"
38
+ )
39
+ end
40
+ @pry.evaluate_ruby(str) if complete_expr
41
+ end
42
+ end
43
+
44
+ result
28
45
  end
29
- end
30
46
 
31
- result
32
- end
47
+ def push(*lines)
48
+ Array(lines).flatten.each do |line|
49
+ @pry.eval(line)
50
+ end
51
+ end
33
52
 
34
- def push(*lines)
35
- Array(lines).flatten.each do |line|
36
- @pry.eval(line)
37
- end
38
- end
53
+ def push_binding(context)
54
+ @pry.push_binding context
55
+ end
39
56
 
40
- def push_binding(context)
41
- @pry.push_binding context
42
- end
57
+ def last_output
58
+ @out.string if @out
59
+ end
43
60
 
44
- def last_output
45
- @out.string if @out
46
- end
61
+ def process_command(command_str)
62
+ @pry.process_command(command_str) || raise("Not a valid command")
63
+ last_command_result_or_output
64
+ end
47
65
 
48
- def process_command(command_str)
49
- @pry.process_command(command_str) or raise "Not a valid command"
50
- last_command_result_or_output
51
- end
66
+ def last_command_result
67
+ result = Pry.current[:pry_cmd_result]
68
+ result.retval if result
69
+ end
52
70
 
53
- def last_command_result
54
- result = Pry.current[:pry_cmd_result]
55
- result.retval if result
56
- end
71
+ protected
57
72
 
58
- protected
73
+ def last_command_result_or_output
74
+ result = last_command_result
75
+ if result != Pry::Command::VOID_VALUE
76
+ result
77
+ else
78
+ last_output
79
+ end
80
+ end
59
81
 
60
- def last_command_result_or_output
61
- result = last_command_result
62
- if result != Pry::Command::VOID_VALUE
63
- result
64
- else
65
- last_output
82
+ def reset_output
83
+ @out = StringIO.new
84
+ @pry.output = @out
85
+ end
66
86
  end
67
87
  end
68
-
69
- def reset_output
70
- @out = StringIO.new
71
- @pry.output = @out
72
- end
73
88
  end
@@ -1,26 +1,34 @@
1
- module Pry::Testable::Utility
2
- #
3
- # Creates a Tempfile then unlinks it after the block has yielded.
4
- #
5
- # @yieldparam [String] file
6
- # The path of the temp file
7
- #
8
- # @return [void]
9
- #
10
- def temp_file(ext = '.rb')
11
- file = Tempfile.open(['pry', ext])
12
- yield file
13
- ensure
14
- file.close(true) if file
15
- end
1
+ # frozen_string_literal: true
16
2
 
17
- def unindent(*args)
18
- Pry::Helpers::CommandHelpers.unindent(*args)
19
- end
3
+ require 'tempfile'
4
+
5
+ class Pry
6
+ module Testable
7
+ module Utility
8
+ #
9
+ # Creates a Tempfile then unlinks it after the block has yielded.
10
+ #
11
+ # @yieldparam [String] file
12
+ # The path of the temp file
13
+ #
14
+ # @return [void]
15
+ #
16
+ def temp_file(ext = '.rb')
17
+ file = Tempfile.open(['pry', ext])
18
+ yield file
19
+ ensure
20
+ file.close(true) if file
21
+ end
22
+
23
+ def unindent(*args)
24
+ Pry::Helpers::CommandHelpers.unindent(*args)
25
+ end
20
26
 
21
- def inner_scope
22
- catch(:inner_scope) do
23
- yield -> { throw(:inner_scope, self) }
27
+ def inner_scope
28
+ catch(:inner_scope) do
29
+ yield -> { throw(:inner_scope, self) }
30
+ end
31
+ end
24
32
  end
25
33
  end
26
34
  end
@@ -1,46 +1,52 @@
1
- module Pry::Testable::Variables
2
- #
3
- # @example
4
- # temporary_constants(:Foo, :Bar) do
5
- # Foo = Class.new(RuntimeError)
6
- # Bar = Class.new(RuntimeError)
7
- # end
8
- # Foo # => NameError
9
- # Bar # => NameError
10
- #
11
- # @param [Array<Symbol>] names
12
- # An array of constant names that be defined by a block,
13
- # and removed by this method afterwards.
14
- #
15
- # @return [void]
16
- #
17
- def temporary_constants(*names)
18
- names.each do |name|
19
- Object.remove_const name if Object.const_defined?(name)
20
- end
21
- yield
22
- ensure
23
- names.each do |name|
24
- Object.remove_const name if Object.const_defined?(name)
25
- end
26
- end
1
+ # frozen_string_literal: true
27
2
 
28
- #
29
- # @param [String] name
30
- # The name of a variable.
31
- #
32
- # @param [String] value
33
- # Its value.
34
- #
35
- # @param [Binding] b
36
- # The binding object to insert a variable into.
37
- #
38
- # @return [void]
39
- #
40
- def insert_variable(name, value, b)
41
- Pry.current[:pry_local] = value
42
- b.eval("#{name} = ::Pry.current[:pry_local]")
43
- ensure
44
- Pry.current[:pry_local] = nil
3
+ class Pry
4
+ module Testable
5
+ module Variables
6
+ #
7
+ # @example
8
+ # temporary_constants(:Foo, :Bar) do
9
+ # Foo = Class.new(RuntimeError)
10
+ # Bar = Class.new(RuntimeError)
11
+ # end
12
+ # Foo # => NameError
13
+ # Bar # => NameError
14
+ #
15
+ # @param [Array<Symbol>] names
16
+ # An array of constant names that be defined by a block,
17
+ # and removed by this method afterwards.
18
+ #
19
+ # @return [void]
20
+ #
21
+ def temporary_constants(*names)
22
+ names.each do |name|
23
+ Object.remove_const name if Object.const_defined?(name)
24
+ end
25
+ yield
26
+ ensure
27
+ names.each do |name|
28
+ Object.remove_const name if Object.const_defined?(name)
29
+ end
30
+ end
31
+
32
+ #
33
+ # @param [String] name
34
+ # The name of a variable.
35
+ #
36
+ # @param [String] value
37
+ # Its value.
38
+ #
39
+ # @param [Binding] binding
40
+ # The binding object to insert a variable into.
41
+ #
42
+ # @return [void]
43
+ #
44
+ def insert_variable(name, value, binding)
45
+ Pry.current[:pry_local] = value
46
+ binding.eval("#{name} = ::Pry.current[:pry_local]")
47
+ ensure
48
+ Pry.current[:pry_local] = nil
49
+ end
50
+ end
45
51
  end
46
52
  end