pry 0.10.2-i386-mingw32 → 1.0.0.pre1-i386-mingw32

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 (189) hide show
  1. data/.document +2 -0
  2. data/.gitignore +16 -0
  3. data/.travis.yml +21 -0
  4. data/.yardopts +3 -0
  5. data/CHANGELOG +503 -0
  6. data/CONTRIBUTORS +55 -0
  7. data/Gemfile +9 -0
  8. data/Guardfile +62 -0
  9. data/LICENSE +2 -2
  10. data/{README.md → README.markdown} +31 -37
  11. data/Rakefile +144 -0
  12. data/TODO +117 -0
  13. data/lib/pry.rb +146 -33
  14. data/lib/pry/cli.rb +13 -35
  15. data/lib/pry/code.rb +63 -24
  16. data/lib/pry/code/loc.rb +2 -2
  17. data/lib/pry/code_object.rb +21 -40
  18. data/lib/pry/command.rb +6 -9
  19. data/lib/pry/command_set.rb +37 -80
  20. data/lib/pry/commands.rb +1 -1
  21. data/lib/pry/commands/amend_line.rb +1 -1
  22. data/lib/pry/commands/bang.rb +1 -1
  23. data/lib/pry/commands/cat.rb +2 -11
  24. data/lib/pry/commands/cat/abstract_formatter.rb +1 -1
  25. data/lib/pry/commands/cat/exception_formatter.rb +7 -6
  26. data/lib/pry/commands/cat/file_formatter.rb +32 -15
  27. data/lib/pry/commands/cat/input_expression_formatter.rb +1 -1
  28. data/lib/pry/commands/cd.rb +3 -14
  29. data/lib/pry/commands/code_collector.rb +4 -4
  30. data/lib/pry/commands/easter_eggs.rb +3 -3
  31. data/lib/pry/commands/edit.rb +22 -10
  32. data/lib/pry/commands/edit/exception_patcher.rb +1 -1
  33. data/lib/pry/commands/edit/file_and_line_locator.rb +2 -0
  34. data/lib/pry/{method/patcher.rb → commands/edit/method_patcher.rb} +37 -40
  35. data/lib/pry/commands/find_method.rb +22 -16
  36. data/lib/pry/commands/gem_install.rb +2 -5
  37. data/lib/pry/commands/gem_open.rb +1 -1
  38. data/lib/pry/commands/gist.rb +11 -10
  39. data/lib/pry/commands/help.rb +14 -14
  40. data/lib/pry/commands/hist.rb +5 -24
  41. data/lib/pry/commands/ls.rb +287 -56
  42. data/lib/pry/commands/play.rb +10 -44
  43. data/lib/pry/commands/pry_backtrace.rb +2 -1
  44. data/lib/pry/commands/raise_up.rb +1 -1
  45. data/lib/pry/commands/reload_code.rb +15 -31
  46. data/lib/pry/commands/ri.rb +3 -7
  47. data/lib/pry/commands/shell_command.rb +12 -17
  48. data/lib/pry/commands/shell_mode.rb +2 -2
  49. data/lib/pry/commands/show_doc.rb +0 -5
  50. data/lib/pry/commands/show_info.rb +10 -11
  51. data/lib/pry/commands/show_source.rb +3 -15
  52. data/lib/pry/commands/simple_prompt.rb +1 -1
  53. data/lib/pry/commands/toggle_color.rb +4 -8
  54. data/lib/pry/commands/whereami.rb +10 -18
  55. data/lib/pry/completion.rb +293 -0
  56. data/lib/pry/config.rb +233 -20
  57. data/lib/pry/core_extensions.rb +19 -29
  58. data/lib/pry/custom_completions.rb +6 -0
  59. data/lib/pry/editor.rb +103 -109
  60. data/lib/pry/helpers/base_helpers.rb +109 -22
  61. data/lib/pry/helpers/command_helpers.rb +8 -10
  62. data/lib/pry/helpers/documentation_helpers.rb +2 -1
  63. data/lib/pry/helpers/text.rb +5 -4
  64. data/lib/pry/history.rb +10 -21
  65. data/lib/pry/history_array.rb +0 -5
  66. data/lib/pry/hooks.rb +29 -9
  67. data/lib/pry/indent.rb +10 -5
  68. data/lib/pry/method.rb +86 -81
  69. data/lib/pry/method/weird_method_locator.rb +2 -4
  70. data/lib/pry/module_candidate.rb +14 -5
  71. data/lib/pry/pager.rb +48 -193
  72. data/lib/pry/plugins.rb +2 -2
  73. data/lib/pry/pry_class.rb +193 -104
  74. data/lib/pry/pry_instance.rb +154 -152
  75. data/lib/pry/rbx_method.rb +13 -0
  76. data/lib/pry/rbx_path.rb +1 -1
  77. data/lib/pry/repl.rb +14 -17
  78. data/lib/pry/repl_file_loader.rb +3 -8
  79. data/lib/pry/rubygem.rb +3 -3
  80. data/lib/pry/terminal.rb +3 -4
  81. data/lib/pry/test/helper.rb +11 -6
  82. data/lib/pry/version.rb +1 -1
  83. data/lib/pry/wrapped_module.rb +56 -49
  84. data/man/pry.1 +195 -0
  85. data/man/pry.1.html +204 -0
  86. data/man/pry.1.ronn +141 -0
  87. data/pry.gemspec +31 -0
  88. data/spec/Procfile +3 -0
  89. data/spec/cli_spec.rb +78 -0
  90. data/spec/code_object_spec.rb +277 -0
  91. data/spec/code_spec.rb +219 -0
  92. data/spec/command_helpers_spec.rb +29 -0
  93. data/spec/command_integration_spec.rb +562 -0
  94. data/spec/command_set_spec.rb +627 -0
  95. data/spec/command_spec.rb +821 -0
  96. data/spec/commands/amend_line_spec.rb +247 -0
  97. data/spec/commands/bang_spec.rb +18 -0
  98. data/spec/commands/cat_spec.rb +164 -0
  99. data/spec/commands/cd_spec.rb +250 -0
  100. data/spec/commands/disable_pry_spec.rb +25 -0
  101. data/spec/commands/edit_spec.rb +725 -0
  102. data/spec/commands/exit_all_spec.rb +27 -0
  103. data/spec/commands/exit_program_spec.rb +19 -0
  104. data/spec/commands/exit_spec.rb +28 -0
  105. data/spec/commands/find_method_spec.rb +70 -0
  106. data/spec/commands/gem_list_spec.rb +26 -0
  107. data/spec/commands/gist_spec.rb +79 -0
  108. data/spec/commands/help_spec.rb +56 -0
  109. data/spec/commands/hist_spec.rb +172 -0
  110. data/spec/commands/jump_to_spec.rb +15 -0
  111. data/spec/commands/ls_spec.rb +189 -0
  112. data/spec/commands/play_spec.rb +136 -0
  113. data/spec/commands/raise_up_spec.rb +56 -0
  114. data/spec/commands/save_file_spec.rb +177 -0
  115. data/spec/commands/show_doc_spec.rb +488 -0
  116. data/spec/commands/show_input_spec.rb +17 -0
  117. data/spec/commands/show_source_spec.rb +760 -0
  118. data/spec/commands/whereami_spec.rb +203 -0
  119. data/spec/completion_spec.rb +221 -0
  120. data/spec/control_d_handler_spec.rb +62 -0
  121. data/spec/documentation_helper_spec.rb +73 -0
  122. data/spec/editor_spec.rb +79 -0
  123. data/spec/exception_whitelist_spec.rb +21 -0
  124. data/spec/fixtures/candidate_helper1.rb +11 -0
  125. data/spec/fixtures/candidate_helper2.rb +8 -0
  126. data/spec/fixtures/example.erb +5 -0
  127. data/spec/fixtures/example_nesting.rb +33 -0
  128. data/spec/fixtures/show_source_doc_examples.rb +15 -0
  129. data/spec/fixtures/testlinkrc +2 -0
  130. data/spec/fixtures/testrc +2 -0
  131. data/spec/fixtures/testrcbad +2 -0
  132. data/spec/fixtures/whereami_helper.rb +6 -0
  133. data/spec/helper.rb +35 -0
  134. data/spec/helpers/bacon.rb +86 -0
  135. data/spec/helpers/mock_pry.rb +44 -0
  136. data/spec/helpers/repl_tester.rb +112 -0
  137. data/spec/helpers/table_spec.rb +105 -0
  138. data/spec/history_array_spec.rb +67 -0
  139. data/spec/hooks_spec.rb +522 -0
  140. data/spec/indent_spec.rb +301 -0
  141. data/spec/method_spec.rb +482 -0
  142. data/spec/prompt_spec.rb +61 -0
  143. data/spec/pry_defaults_spec.rb +420 -0
  144. data/spec/pry_history_spec.rb +69 -0
  145. data/spec/pry_output_spec.rb +95 -0
  146. data/spec/pry_repl_spec.rb +86 -0
  147. data/spec/pry_spec.rb +394 -0
  148. data/spec/pryrc_spec.rb +97 -0
  149. data/spec/run_command_spec.rb +25 -0
  150. data/spec/sticky_locals_spec.rb +147 -0
  151. data/spec/syntax_checking_spec.rb +81 -0
  152. data/spec/wrapped_module_spec.rb +261 -0
  153. data/wiki/Customizing-pry.md +397 -0
  154. data/wiki/Home.md +4 -0
  155. metadata +272 -61
  156. checksums.yaml +0 -7
  157. data/CHANGELOG.md +0 -714
  158. data/lib/pry/code/code_file.rb +0 -103
  159. data/lib/pry/color_printer.rb +0 -55
  160. data/lib/pry/commands/change_inspector.rb +0 -27
  161. data/lib/pry/commands/change_prompt.rb +0 -26
  162. data/lib/pry/commands/list_inspectors.rb +0 -35
  163. data/lib/pry/commands/list_prompts.rb +0 -35
  164. data/lib/pry/commands/ls/constants.rb +0 -47
  165. data/lib/pry/commands/ls/formatter.rb +0 -49
  166. data/lib/pry/commands/ls/globals.rb +0 -48
  167. data/lib/pry/commands/ls/grep.rb +0 -21
  168. data/lib/pry/commands/ls/instance_vars.rb +0 -39
  169. data/lib/pry/commands/ls/interrogatable.rb +0 -18
  170. data/lib/pry/commands/ls/jruby_hacks.rb +0 -49
  171. data/lib/pry/commands/ls/local_names.rb +0 -35
  172. data/lib/pry/commands/ls/local_vars.rb +0 -39
  173. data/lib/pry/commands/ls/ls_entity.rb +0 -70
  174. data/lib/pry/commands/ls/methods.rb +0 -57
  175. data/lib/pry/commands/ls/methods_helper.rb +0 -46
  176. data/lib/pry/commands/ls/self_methods.rb +0 -32
  177. data/lib/pry/commands/watch_expression.rb +0 -105
  178. data/lib/pry/commands/watch_expression/expression.rb +0 -38
  179. data/lib/pry/config/behavior.rb +0 -139
  180. data/lib/pry/config/convenience.rb +0 -25
  181. data/lib/pry/config/default.rb +0 -161
  182. data/lib/pry/exceptions.rb +0 -78
  183. data/lib/pry/input_completer.rb +0 -242
  184. data/lib/pry/input_lock.rb +0 -132
  185. data/lib/pry/inspector.rb +0 -27
  186. data/lib/pry/last_exception.rb +0 -61
  187. data/lib/pry/object_path.rb +0 -82
  188. data/lib/pry/output.rb +0 -50
  189. data/lib/pry/prompt.rb +0 -26
@@ -15,6 +15,14 @@ class Pry
15
15
  file.close(true) if file
16
16
  end
17
17
 
18
+ def render_output(str, opts={})
19
+ if opts[:flood]
20
+ output.puts str
21
+ else
22
+ stagger_output str
23
+ end
24
+ end
25
+
18
26
  def internal_binding?(target)
19
27
  m = target.eval("::Kernel.__method__").to_s
20
28
  # class_eval is here because of http://jira.codehaus.org/browse/JRUBY-6753
@@ -141,16 +149,6 @@ class Pry
141
149
 
142
150
  Range.new(a, b)
143
151
  end
144
-
145
- def set_file_and_dir_locals(file_name, _pry_=_pry_(), target=target())
146
- return if !target or !file_name
147
- _pry_.last_file = File.expand_path(file_name)
148
- _pry_.inject_local("_file_", _pry_.last_file, target)
149
-
150
- _pry_.last_dir = File.dirname(_pry_.last_file)
151
- _pry_.inject_local("_dir_", _pry_.last_dir, target)
152
- end
153
152
  end
154
-
155
153
  end
156
154
  end
@@ -8,6 +8,7 @@ class Pry
8
8
  module_function
9
9
 
10
10
  def process_rdoc(comment)
11
+ return comment unless Pry.color
11
12
  comment = comment.dup
12
13
  comment.gsub(/<code>(?:\s*\n)?(.*?)\s*<\/code>/m) { CodeRay.scan($1, :ruby).term }.
13
14
  gsub(/<em>(?:\s*\n)?(.*?)\s*<\/em>/m) { "\e[1m#{$1}\e[0m" }.
@@ -36,7 +37,7 @@ class Pry
36
37
  yard_tags = ["param", "return", "option", "yield", "attr", "attr_reader", "attr_writer",
37
38
  "deprecate", "example", "raise"]
38
39
  (yard_tags - ["example"]).inject(comment) { |a, v| process_yardoc_tag(a, v) }.
39
- gsub(/^@(#{yard_tags.join("|")})/) { "\e[33m#{$1}\e[0m" }
40
+ gsub(/^@(#{yard_tags.join("|")})/) { Pry.color ? "\e[33m#{$1}\e[0m": $1 }
40
41
  end
41
42
 
42
43
  def process_comment_markup(comment)
@@ -21,11 +21,11 @@ class Pry
21
21
 
22
22
  COLORS.each_pair do |color, value|
23
23
  define_method color do |text|
24
- "\033[0;#{30+value}m#{text}\033[0m"
24
+ Pry.color ? "\033[0;#{30+value}m#{text}\033[0m" : text.to_s
25
25
  end
26
26
 
27
27
  define_method "bright_#{color}" do |text|
28
- "\033[1;#{30+value}m#{text}\033[0m"
28
+ Pry.color ? "\033[1;#{30+value}m#{text}\033[0m" : text.to_s
29
29
  end
30
30
  end
31
31
 
@@ -38,11 +38,12 @@ class Pry
38
38
  end
39
39
 
40
40
  # Returns _text_ as bold text for use on a terminal.
41
+ # _Pry.color_ must be true for this method to perform any transformations.
41
42
  #
42
43
  # @param [String, #to_s] text
43
44
  # @return [String] _text_
44
45
  def bold(text)
45
- "\e[1m#{text}\e[0m"
46
+ Pry.color ? "\e[1m#{text}\e[0m" : text.to_s
46
47
  end
47
48
 
48
49
  # Returns `text` in the default foreground colour.
@@ -55,7 +56,7 @@ class Pry
55
56
  end
56
57
  alias_method :bright_default, :bold
57
58
 
58
- # Executes the block with `Pry.config.color` set to false.
59
+ # Executes the block with `Pry.color` set to false.
59
60
  # @yield
60
61
  # @return [void]
61
62
  def no_color(&block)
@@ -16,18 +16,10 @@ class Pry
16
16
 
17
17
  # Assign the default methods for loading, saving, pushing, and clearing.
18
18
  def restore_default_behavior
19
- Pry.config.input # force Readline to load if applicable
20
-
21
- @loader = method(:read_from_file)
22
- @saver = method(:save_to_file)
23
-
24
- if defined?(Readline)
25
- @pusher = method(:push_to_readline)
26
- @clearer = method(:clear_readline)
27
- else
28
- @pusher = proc { }
29
- @clearer = proc { }
30
- end
19
+ @loader = method(:read_from_file)
20
+ @saver = method(:save_to_file)
21
+ @pusher = method(:push_to_readline)
22
+ @clearer = method(:clear_readline)
31
23
  end
32
24
 
33
25
  # Load the input history using `History.loader`.
@@ -36,7 +28,6 @@ class Pry
36
28
  @loader.call do |line|
37
29
  @pusher.call(line.chomp)
38
30
  @history << line.chomp
39
- @original_lines += 1
40
31
  end
41
32
  end
42
33
 
@@ -81,10 +72,10 @@ class Pry
81
72
 
82
73
  # The default loader. Yields lines from `Pry.history.config.file`.
83
74
  def read_from_file
84
- path = history_file_path
75
+ filename = File.expand_path(Pry.config.history.file)
85
76
 
86
- if File.exists?(path)
87
- File.foreach(path) { |line| yield(line) }
77
+ if File.exists?(filename)
78
+ File.foreach(filename) { |line| yield(line) }
88
79
  end
89
80
  rescue => error
90
81
  warn "History file not loaded: #{error.message}"
@@ -111,17 +102,15 @@ class Pry
111
102
  if defined?(@history_file)
112
103
  @history_file
113
104
  else
114
- @history_file = File.open(history_file_path, 'a', 0600).tap do |file|
115
- file.sync = true
116
- end
105
+ @history_file = File.open(file_path, 'a').tap { |f| f.sync = true }
117
106
  end
118
107
  rescue Errno::EACCES
119
108
  warn 'History not saved; unable to open your history file for writing.'
120
109
  @history_file = false
121
110
  end
122
111
 
123
- def history_file_path
124
- File.expand_path(@file_path || Pry.config.history.file)
112
+ def file_path
113
+ @file_path || Pry.config.history.file
125
114
  end
126
115
  end
127
116
  end
@@ -89,11 +89,6 @@ class Pry
89
89
  ((@count - size)...@count).map { |n| @hash[n] }
90
90
  end
91
91
 
92
- # Returns [Hash] copy of the internal @hash history
93
- def to_h
94
- @hash.dup
95
- end
96
-
97
92
  def pop!
98
93
  @hash.delete @count - 1
99
94
  @count -= 1
@@ -18,11 +18,11 @@ class Pry
18
18
  # @param [Hash] hash The hash to convert to `Pry::Hooks`.
19
19
  # @return [Pry::Hooks] The resulting `Pry::Hooks` instance.
20
20
  def self.from_hash(hash)
21
- return hash if hash.instance_of?(self)
22
21
  instance = new
23
22
  hash.each do |k, v|
24
23
  instance.add_hook(k, nil, v)
25
24
  end
25
+
26
26
  instance
27
27
  end
28
28
 
@@ -49,6 +49,22 @@ class Pry
49
49
  @errors ||= []
50
50
  end
51
51
 
52
+ # FIXME:
53
+ # This is a hack to alert people of the new API.
54
+ def [](event_name)
55
+ warn "`Pry.hooks[]` is deprecated! Please use the new `Pry::Hooks` API! http://rubydoc.info/github/pry/pry/master/Pry/Hooks"
56
+
57
+ get_hook(event_name, nil)
58
+ end
59
+
60
+ # FIXME:
61
+ # This is a hack to alert people of the new API.
62
+ def []=(event_name, callable)
63
+ warn "`Pry.hooks[]=` is deprecated! Please use the new `Pry::Hooks` API! http://rubydoc.info/github/pry/pry/master/Pry/Hooks"
64
+
65
+ add_hook(event_name, nil, callable)
66
+ end
67
+
52
68
  # Destructively merge the contents of two `Pry:Hooks` instances.
53
69
  # @param [Pry::Hooks] other The `Pry::Hooks` instance to merge
54
70
  # @return [Pry:Hooks] Returns the receiver.
@@ -130,14 +146,18 @@ class Pry
130
146
  def exec_hook(event_name, *args, &block)
131
147
  @hooks[event_name] ||= []
132
148
 
133
- @hooks[event_name].map do |hook_name, callable|
134
- begin
135
- callable.call(*args, &block)
136
- rescue RescuableException => e
137
- errors << e
138
- e
139
- end
140
- end.last
149
+ # silence warnings to get rid of 1.8's "warning: multiple values
150
+ # for a block parameter" warnings
151
+ Pry::Helpers::BaseHelpers.silence_warnings do
152
+ @hooks[event_name].map do |hook_name, callable|
153
+ begin
154
+ callable.call(*args, &block)
155
+ rescue RescuableException => e
156
+ errors << e
157
+ e
158
+ end
159
+ end.last
160
+ end
141
161
  end
142
162
 
143
163
  # Return the number of hook functions registered for the `event_name` event.
@@ -94,8 +94,8 @@ class Pry
94
94
  indent = new
95
95
  lines = str.split("\n")
96
96
  n = line_number - 1
97
- to_indent = lines[0...n] << (lines[n] || "").split("def").first(1)
98
- indent.indent(to_indent.join("\n") << "\n")
97
+ to_indent = lines[0...n] + (lines[n] || "").split("def").first(1)
98
+ indent.indent(to_indent.join("\n") + "\n")
99
99
  indent.module_nesting
100
100
  end
101
101
 
@@ -320,7 +320,7 @@ class Pry
320
320
  # [ ["class", "Foo"], ["module", "Bar::Baz"], ["class <<", "self"] ]
321
321
  #
322
322
  # A nil value in the @module_nesting array happens in two places: either
323
- # when @awaiting_class is true and we're still waiting for the string to
323
+ # when @awaiting_token is true and we're still waiting for the string to
324
324
  # fill that space, or when a parse was rejected.
325
325
  #
326
326
  # At the moment this function is quite restricted about what formats it will
@@ -341,7 +341,7 @@ class Pry
341
341
  @module_nesting.last[1] = token if kind == :class
342
342
  @awaiting_class = false
343
343
  else
344
- # leave @module_nesting[-1]
344
+ # leave @nesting[-1][
345
345
  @awaiting_class = false
346
346
  end
347
347
  end
@@ -384,7 +384,7 @@ class Pry
384
384
  # @return [String]
385
385
  def correct_indentation(prompt, code, overhang=0)
386
386
  prompt = prompt.delete("\001\002")
387
- line_to_measure = Pry::Helpers::Text.strip_color(prompt) << code
387
+ line_to_measure = Pry::Helpers::Text.strip_color(prompt) + code
388
388
  whitespace = ' ' * overhang
389
389
 
390
390
  _, cols = Terminal.screen_size
@@ -402,5 +402,10 @@ class Pry
402
402
 
403
403
  "#{move_up}#{prompt}#{colorize_code(code)}#{whitespace}#{move_down}"
404
404
  end
405
+
406
+ # Given the current Pry environment, should we try to correct indentation?
407
+ def should_correct_indentation?
408
+ Pry::Helpers::BaseHelpers.use_ansi_codes? && Pry.config.correct_indent
409
+ end
405
410
  end
406
411
  end
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require 'pry/helpers/documentation_helpers'
2
3
 
3
4
  class Pry
@@ -18,10 +19,10 @@ class Pry
18
19
  class Method
19
20
  require 'pry/method/weird_method_locator'
20
21
  require 'pry/method/disowned'
21
- require 'pry/method/patcher'
22
22
 
23
23
  extend Helpers::BaseHelpers
24
24
  include Helpers::BaseHelpers
25
+ include RbxMethod if rbx?
25
26
  include Helpers::DocumentationHelpers
26
27
  include CodeObject::Helpers
27
28
 
@@ -30,7 +31,8 @@ class Pry
30
31
  # search in, find and return the requested method wrapped in a `Pry::Method`
31
32
  # instance.
32
33
  #
33
- # @param [String] name The name of the method to retrieve.
34
+ # @param [String, nil] name The name of the method to retrieve, or `nil` to
35
+ # delegate to `from_binding` instead.
34
36
  # @param [Binding] target The context in which to search for the method.
35
37
  # @param [Hash] options
36
38
  # @option options [Boolean] :instance Look for an instance method if `name` doesn't
@@ -38,16 +40,13 @@ class Pry
38
40
  # @option options [Boolean] :methods Look for a bound/singleton method if `name` doesn't
39
41
  # contain any context.
40
42
  # @return [Pry::Method, nil] A `Pry::Method` instance containing the requested
41
- # method, or `nil` if name is `nil` or no method could be located matching the parameters.
43
+ # method, or `nil` if no method could be located matching the parameters.
42
44
  def from_str(name, target=TOPLEVEL_BINDING, options={})
43
45
  if name.nil?
44
- nil
46
+ from_binding(target)
45
47
  elsif name.to_s =~ /(.+)\#(\S+)\Z/
46
48
  context, meth_name = $1, $2
47
49
  from_module(target.eval(context), meth_name, target)
48
- elsif name.to_s =~ /(.+)(\[\])\Z/
49
- context, meth_name = $1, $2
50
- from_obj(target.eval(context), meth_name, target)
51
50
  elsif name.to_s =~ /(.+)(\.|::)(\S+)\Z/
52
51
  context, meth_name = $1, $3
53
52
  from_obj(target.eval(context), meth_name, target)
@@ -140,34 +139,15 @@ class Pry
140
139
  # @param [Boolean] include_super Whether to include methods from ancestors.
141
140
  # @return [Array[Pry::Method]]
142
141
  def all_from_class(klass, include_super=true)
143
- %w(public protected private).map do |visibility|
144
- safe_send(klass, :"#{visibility}_instance_methods", include_super).map do |method_name|
145
- new(safe_send(klass, :instance_method, method_name), :visibility => visibility.to_sym)
146
- end
147
- end.flatten(1)
142
+ all_from_common(klass, :instance_method, include_super)
148
143
  end
149
144
 
150
- #
151
145
  # Get all of the methods on an `Object`
152
- #
153
146
  # @param [Object] obj
154
- #
155
- # @param [Boolean] include_super
156
- # indicates whether or not to include methods from ancestors.
157
- #
147
+ # @param [Boolean] include_super Whether to include methods from ancestors.
158
148
  # @return [Array[Pry::Method]]
159
- #
160
149
  def all_from_obj(obj, include_super=true)
161
- all_from_class(singleton_class_of(obj), include_super)
162
- end
163
-
164
- #
165
- # @deprecated
166
- # please use {#all_from_obj} instead.
167
- # the `method_type` argument is ignored.
168
- #
169
- def all_from_common(obj, method_type = nil, include_super=true)
170
- all_from_obj(obj, include_super)
150
+ all_from_common(obj, :method, include_super)
171
151
  end
172
152
 
173
153
  # Get every `Class` and `Module`, in order, that will be checked when looking
@@ -178,7 +158,7 @@ class Pry
178
158
  if Class === obj
179
159
  singleton_class_resolution_order(obj) + instance_resolution_order(Class)
180
160
  else
181
- klass = singleton_class_of(obj) rescue obj.class
161
+ klass = singleton_class(obj) rescue obj.class
182
162
  instance_resolution_order(klass)
183
163
  end
184
164
  end
@@ -199,11 +179,26 @@ class Pry
199
179
  end
200
180
 
201
181
  def singleton_method_definition?(name, definition_line)
202
- /^define_singleton_method\(?\s*[:\"\']#{Regexp.escape(name)}|^def\s*self\.#{Regexp.escape(name)}/ =~ definition_line.strip
182
+ /^define_singleton_method\(?\s*[:\"\']#{name}|^def\s*self\.#{name}/ =~ definition_line.strip
203
183
  end
204
184
 
205
185
  def instance_method_definition?(name, definition_line)
206
- /^define_method\(?\s*[:\"\']#{Regexp.escape(name)}|^def\s*#{Regexp.escape(name)}/ =~ definition_line.strip
186
+ /^define_method\(?\s*[:\"\']#{name}|^def\s*#{name}/ =~ definition_line.strip
187
+ end
188
+
189
+ private
190
+
191
+ # See all_from_class and all_from_obj.
192
+ # If method_type is :instance_method, obj must be a `Class` or a `Module`
193
+ # If method_type is :method, obj can be any `Object`
194
+ #
195
+ # N.B. we pre-cache the visibility here to avoid O(N²) behaviour in "ls".
196
+ def all_from_common(obj, method_type, include_super=true)
197
+ %w(public protected private).map do |visibility|
198
+ safe_send(obj, :"#{visibility}_#{method_type}s", include_super).map do |method_name|
199
+ new(safe_send(obj, method_type, method_name), :visibility => visibility.to_sym)
200
+ end
201
+ end.flatten(1)
207
202
  end
208
203
 
209
204
  # Get the singleton classes of superclasses that could define methods on
@@ -211,21 +206,14 @@ class Pry
211
206
  # If a module is included at multiple points in the ancestry, only
212
207
  # the lowest copy will be returned.
213
208
  def singleton_class_resolution_order(klass)
214
- ancestors = Pry::Method.safe_send(klass, :ancestors)
215
- resolution_order = ancestors.grep(Class).map do |anc|
216
- [singleton_class_of(anc), *singleton_class_of(anc).included_modules]
217
- end.flatten(1)
209
+ resolution_order = Pry::Method.safe_send(klass, :ancestors).map do |anc|
210
+ [singleton_class(anc)] + singleton_class(anc).included_modules if anc.is_a?(Class)
211
+ end.compact.flatten(1)
218
212
 
219
213
  resolution_order.reverse.uniq.reverse - Class.included_modules
220
214
  end
221
215
 
222
- def singleton_class_of(obj)
223
- begin
224
- class << obj; self; end
225
- rescue TypeError # can't define singleton. Fixnum, Symbol, Float, ...
226
- obj.class
227
- end
228
- end
216
+ def singleton_class(obj); class << obj; self; end end
229
217
  end
230
218
 
231
219
  # A new instance of `Pry::Method` wrapping the given `::Method`, `UnboundMethod`, or `Proc`.
@@ -275,18 +263,26 @@ class Pry
275
263
  def source
276
264
  @source ||= case source_type
277
265
  when :c
278
- c_source
266
+ info = pry_doc_info
267
+ if info and info.source
268
+ code = strip_comments_from_c_code(info.source)
269
+ end
279
270
  when :ruby
280
- ruby_source
271
+ # clone of MethodSource.source_helper that knows to use our
272
+ # hacked version of source_location for rbx core methods, and
273
+ # our input buffer for methods defined in (pry)
274
+ file, line = *source_location
275
+ raise SourceNotFoundError, "Could not locate source for #{name_with_owner}!" unless file
276
+
277
+ begin
278
+ code = Pry::Code.from_file(file).expression_at(line)
279
+ rescue SyntaxError => e
280
+ raise MethodSource::SourceNotFoundError.new(e.message)
281
+ end
282
+ strip_leading_whitespace(code)
281
283
  end
282
284
  end
283
285
 
284
- # Update the live copy of the method's source.
285
- def redefine(source)
286
- Patcher.new(self).patch_in_ram source
287
- Pry::Method(owner.instance_method(name))
288
- end
289
-
290
286
  # Can we get the source code for this method?
291
287
  # @return [Boolean]
292
288
  def source?
@@ -297,13 +293,20 @@ class Pry
297
293
 
298
294
  # @return [String, nil] The documentation for the method, or `nil` if it's
299
295
  # unavailable.
296
+ # @raise [CommandError] Raises when the method was defined in the REPL.
300
297
  def doc
301
298
  @doc ||= case source_type
302
299
  when :c
303
300
  info = pry_doc_info
304
301
  info.docstring if info
305
302
  when :ruby
306
- get_comment_content(comment)
303
+ if rbx? && !pry_method?
304
+ get_comment_content(core_doc)
305
+ elsif pry_method?
306
+ get_comment_content(doc_for_pry_method)
307
+ else
308
+ get_comment_content(@method.comment)
309
+ end
307
310
  end
308
311
  end
309
312
 
@@ -313,6 +316,15 @@ class Pry
313
316
  source_location.nil? ? :c : :ruby
314
317
  end
315
318
 
319
+ def source_location
320
+ if @method.source_location && rbx?
321
+ file, line = @method.source_location
322
+ [RbxPath.convert_path_to_full(file), line]
323
+ else
324
+ @method.source_location
325
+ end
326
+ end
327
+
316
328
  # @return [String, nil] The name of the file the method is defined in, or
317
329
  # `nil` if the filename is unavailable.
318
330
  def source_file
@@ -422,14 +434,15 @@ class Pry
422
434
  end
423
435
 
424
436
  # @return [Array<String>] All known aliases for the method.
437
+ # @note On Ruby 1.8 this method always returns an empty Array for methods
438
+ # implemented in C.
425
439
  def aliases
426
440
  owner = @method.owner
427
441
  # Avoid using `to_sym` on {Method#name}, which returns a `String`, because
428
442
  # it won't be garbage collected.
429
443
  name = @method.name
430
444
 
431
- all_methods_to_compare = owner.instance_methods | owner.private_instance_methods
432
- alias_list = all_methods_to_compare.combination(2).select do |pair|
445
+ alias_list = owner.instance_methods.combination(2).select do |pair|
433
446
  pair.include?(name) &&
434
447
  owner.instance_method(pair.first) == owner.instance_method(pair.last)
435
448
  end.flatten
@@ -470,10 +483,6 @@ class Pry
470
483
  @method.send(method_name, *args, &block)
471
484
  end
472
485
 
473
- def comment
474
- Pry::Code.from_file(source_file).comment_describing(source_line)
475
- end
476
-
477
486
  private
478
487
 
479
488
  # @return [YARD::CodeObjects::MethodObject]
@@ -483,13 +492,31 @@ class Pry
483
492
  Pry::MethodInfo.info_for(@method) or raise CommandError, "Cannot locate this method: #{name}. (source_location returns nil)"
484
493
  else
485
494
  fail_msg = "Cannot locate this method: #{name}."
486
- if mri?
495
+ if mri_18? || mri_19?
487
496
  fail_msg += ' Try `gem-install pry-doc` to get access to Ruby Core documentation.'
488
497
  end
489
498
  raise CommandError, fail_msg
490
499
  end
491
500
  end
492
501
 
502
+ # FIXME: a very similar method to this exists on WrappedModule: extract_doc_for_candidate
503
+ def doc_for_pry_method
504
+ _, line_num = source_location
505
+
506
+ buffer = ""
507
+ Pry.line_buffer[0..(line_num - 1)].each do |line|
508
+ # Add any line that is a valid ruby comment,
509
+ # but clear as soon as we hit a non comment line.
510
+ if (line =~ /^\s*#/) || (line =~ /^\s*$/)
511
+ buffer << line.lstrip
512
+ else
513
+ buffer.replace("")
514
+ end
515
+ end
516
+
517
+ buffer
518
+ end
519
+
493
520
  # @param [Class, Module] ancestors The ancestors to investigate
494
521
  # @return [Method] The unwrapped super-method
495
522
  def super_using_ancestors(ancestors, times=1)
@@ -520,27 +547,5 @@ class Pry
520
547
 
521
548
  nil
522
549
  end
523
-
524
- def c_source
525
- info = pry_doc_info
526
- if info and info.source
527
- strip_comments_from_c_code(info.source)
528
- end
529
- end
530
-
531
- def ruby_source
532
- # clone of MethodSource.source_helper that knows to use our
533
- # hacked version of source_location for rbx core methods, and
534
- # our input buffer for methods defined in (pry)
535
- file, line = *source_location
536
- raise SourceNotFoundError, "Could not locate source for #{name_with_owner}!" unless file
537
-
538
- begin
539
- code = Pry::Code.from_file(file).expression_at(line)
540
- rescue SyntaxError => e
541
- raise MethodSource::SourceNotFoundError.new(e.message)
542
- end
543
- strip_leading_whitespace(code)
544
- end
545
550
  end
546
551
  end