pry 0.5.2 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -93,11 +93,11 @@ an instance variable inside that class:
93
93
  pry(main)* @x = 20
94
94
  pry(main)* end
95
95
  => 20
96
- pry(main)> Hello.pry
96
+ pry(main)> cd Hello
97
97
  Beginning Pry session for Hello
98
98
  pry(Hello):1> instance_variables
99
99
  => [:@x]
100
- pry(Hello):1> @x.pry
100
+ pry(Hello):1> cd @x
101
101
  Beginning Pry session for 20
102
102
  pry(20:2)> self + 10
103
103
  => 30
@@ -163,6 +163,7 @@ end.
163
163
  * Use `_pry_` to reference the Pry instance managing the current session.
164
164
  * Pry supports tab completion.
165
165
  * Pry has multi-line support built in.
166
+ * Use `^d` (control-d) to quickly break out of a session.
166
167
  * Pry has special commands not found in many other Ruby REPLs: `show-method`, `show-doc`
167
168
  `jump-to`, `ls`, `cd`, `cat`
168
169
  * Pry gives good control over nested sessions (important when exploring complicated runtime state)
@@ -220,6 +221,9 @@ it returns the result of the evaluation or an Exception object in
220
221
  case of error. It also takes the same parameters as `Pry#repl()`
221
222
  * Similarly `Pry#r()` only performs the Read section of the REPL, only
222
223
  returning the Ruby expression (as a string). It takes the same parameters as all the others.
224
+ * `Pry.run_command COMMAND` enables you to invoke Pry commands outside
225
+ of a session, e.g `Pry.run_command "ls -m", :context => MyObject`. See
226
+ docs for more info.
223
227
 
224
228
  ### Session commands
225
229
 
@@ -244,17 +248,11 @@ If you want to access a method of the same name, prefix the invocation by whites
244
248
  * `cd VAR` Starts a `Pry` session on the variable VAR. E.g `cd @x`
245
249
  (use `cd ..` to go back).
246
250
  * `show-method [OPTIONS] METH` Displays the sourcecode for the method
247
- `METH`. E.g `show-method hello`. See `show-method --help` for more info.
251
+ `METH`. e.g `show-method hello`. See `show-method --help` for more info.
248
252
  * `show-doc [OPTIONS] METH` Displays comments for `METH`. See `show-doc
249
253
  --help` for more info.
250
- * `exit-program` or `quit-program` will end the currently running
251
- program.
252
- * `nesting` Shows Pry nesting information.
253
- * `cat-file FILE` Displays the contents of a file on disk in the Pry session.
254
- * `eval-file [OPTIONS] FILE` Evals a Ruby script at top-level or in
255
- the current context. See `eval-file --help` for more info.
256
- * `!pry` Starts a Pry session on the implied receiver; this can be
257
- used in the middle of an expression in multi-line input.
254
+ * `show-command COMMAND` Displays the sourcecode for the given Pry
255
+ command. e.g: `show-command cd`
258
256
  * `jump-to NEST_LEVEL` Unwinds the Pry stack (nesting level) until the appropriate nesting level is reached.
259
257
  * `exit-all` breaks out of all Pry nesting levels and returns to the
260
258
  calling process.
@@ -16,6 +16,12 @@ class Pry
16
16
  meth_name
17
17
  end
18
18
  end
19
+
20
+ check_for_dynamically_defined_method = lambda do |file|
21
+ if file =~ /(\(.*\))|<.*>/
22
+ raise "Cannot retrieve source for dynamically defined method."
23
+ end
24
+ end
19
25
 
20
26
  command "!", "Clear the input buffer. Useful if the parsing process goes wrong and you get stuck in the read loop." do
21
27
  output.puts "Input buffer cleared!"
@@ -72,7 +78,7 @@ class Pry
72
78
 
73
79
  command "ls", "Show the list of vars in the current scope. Type `ls --help` for more info." do |*args|
74
80
  options = {}
75
-
81
+
76
82
  # Set target local to the default -- note that we can set a different target for
77
83
  # ls if we like: e.g ls my_var
78
84
  target = target()
@@ -111,15 +117,15 @@ Shows local and instance variables by default.
111
117
  options[:M] = true
112
118
  end
113
119
 
114
- opts.on("-P", "--public", "Display public methods.") do
120
+ opts.on("-P", "--public", "Display public methods (with -m).") do
115
121
  options[:P] = true
116
122
  end
117
123
 
118
- opts.on("-r", "--protected", "Display protected methods.") do
124
+ opts.on("-r", "--protected", "Display protected methods (with -m).") do
119
125
  options[:r] = true
120
126
  end
121
127
 
122
- opts.on("-p", "--private", "Display private methods.") do
128
+ opts.on("-p", "--private", "Display private methods (with -m).") do
123
129
  options[:p] = true
124
130
  end
125
131
 
@@ -172,10 +178,10 @@ Shows local and instance variables by default.
172
178
  info["instance variables"] = [Array(target.eval("instance_variables")).sort, i += 1] if options[:i] || options[:a]
173
179
 
174
180
  info["class variables"] = [if target_self.is_a?(Module)
175
- Array(target.eval("class_variables")).sort
176
- else
177
- Array(target.eval("self.class.class_variables")).sort
178
- end, i += 1] if options[:k] || options[:a]
181
+ Array(target.eval("class_variables")).sort
182
+ else
183
+ Array(target.eval("self.class.class_variables")).sort
184
+ end, i += 1] if options[:k] || options[:a]
179
185
 
180
186
  info["global variables"] = [Array(target.eval("global_variables")).sort, i += 1] if options[:g] || options[:a]
181
187
 
@@ -196,9 +202,9 @@ Shows local and instance variables by default.
196
202
  if Module.method(:constants).arity == 0
197
203
  csuper = nil
198
204
  end
199
-
205
+
200
206
  info["constants"] = [Array(target_self.is_a?(Module) ? target.eval("constants(#{csuper})") :
201
- target.eval("self.class.constants(#{csuper})")).uniq.sort, i += 1] if options[:c] || options[:a]
207
+ target.eval("self.class.constants(#{csuper})")).uniq.sort, i += 1] if options[:c] || options[:a]
202
208
 
203
209
  # verbose output?
204
210
  if options[:v]
@@ -212,9 +218,11 @@ Shows local and instance variables by default.
212
218
  end
213
219
  end
214
220
 
215
- # plain
221
+ # plain
216
222
  else
217
- output.puts Pry.view(info.values.sort_by { |v| v.last }.map { |v| v.first }.inject(&:+))
223
+ list = info.values.sort_by { |v| v.last }.map { |v| v.first }.inject(&:+)
224
+ output.puts Pry.view(list)
225
+ list
218
226
  end
219
227
  end
220
228
 
@@ -264,8 +272,8 @@ e.g: eval-file -c "hello.rb"
264
272
  TOPLEVEL_BINDING.eval(File.read(file_name))
265
273
  output.puts "--\nEval'd '#{file_name}' at top-level."
266
274
  end
267
- new_constants = Object.constants - old_constants
268
- output.puts "Brought in the following top-level constants: #{new_constants.inspect}" if !new_constants.empty?
275
+ new_constants = Object.constants - old_constants
276
+ output.puts "Brought in the following top-level constants: #{new_constants.inspect}" if !new_constants.empty?
269
277
  end
270
278
 
271
279
  command "cat", "Show output of VAR.inspect. Aliases: inspect" do |obj|
@@ -329,8 +337,10 @@ e.g show-doc hello_method
329
337
  next
330
338
  end
331
339
 
332
- doc = meth.comment
333
340
  file, line = meth.source_location
341
+ check_for_dynamically_defined_method.call(file)
342
+ doc = meth.comment
343
+
334
344
  output.puts "--\nFrom #{file} @ line ~#{line}:\n--"
335
345
  output.puts doc
336
346
  end
@@ -378,26 +388,31 @@ e.g: show-method hello_method
378
388
  next
379
389
  end
380
390
 
381
- code = meth.source
382
391
  file, line = meth.source_location
392
+ check_for_dynamically_defined_method.call(file)
393
+ code = meth.source
394
+
383
395
  output.puts "--\nFrom #{file} @ line #{line}:\n--"
384
396
  output.puts code
397
+ code
385
398
  end
386
399
 
387
- command "show-command", "Show sourcecode for a Pry command, e.g: show-command ls" do |command_name|
388
- cmds = Pry.active_instance.commands.commands
389
-
400
+ command "show-command", "Show sourcecode for a Pry command, e.g: show-command cd" do |command_name|
390
401
  if !command_name
391
402
  output.puts "You must provide a command name."
392
403
  next
393
404
  end
394
405
 
395
- if cmds[command_name]
396
- meth = cmds[command_name][:action]
397
- code = meth.source
406
+ if commands[command_name]
407
+ meth = commands[command_name][:action]
408
+
398
409
  file, line = meth.source_location
410
+ check_for_dynamically_defined_method.call(file)
411
+ code = meth.source
412
+
399
413
  output.puts "--\nFrom #{file} @ line #{line}:\n--"
400
414
  output.puts code
415
+ code
401
416
  else
402
417
  output.puts "No such command: #{command_name}."
403
418
  end
@@ -1,4 +1,5 @@
1
1
  require 'readline'
2
+ require 'shellwords'
2
3
 
3
4
  # @author John Mair (banisterfiend)
4
5
  class Pry
@@ -94,6 +95,47 @@ class Pry
94
95
  end
95
96
  end
96
97
 
98
+ # Run a Pry command from outside a session. The commands available are
99
+ # those referenced by `Pry.commands` (the default command set).
100
+ # Command output is suppresed by default, this is because the return
101
+ # value (if there is one) is likely to be more useful.
102
+ # @param [String] arg_string The Pry command (including arguments,
103
+ # if any).
104
+ # @param [Hash] options Optional named parameters.
105
+ # @option options [Object, Binding] :context The object context to run the
106
+ # command under. Defaults to `TOPLEVEL_BINDING` (main).
107
+ # @option options [Boolean] :show_output Whether to show command
108
+ # output. Defaults to false.
109
+ # @example Run at top-level with no output.
110
+ # Pry.run_command "ls"
111
+ # @example Run under Pry class, returning only public methods.
112
+ # Pry.run_command "ls -m", :context => Pry
113
+ # @example Display command output.
114
+ # Pry.run_command "ls -av", :show_output => true
115
+ def self.run_command(arg_string, options={})
116
+ name, arg_string = arg_string.split(/\s+/, 2)
117
+ arg_string = "" if !arg_string
118
+
119
+ options = {
120
+ :context => TOPLEVEL_BINDING,
121
+ :show_output => false
122
+ }.merge!(options)
123
+
124
+ null_output = Object.new.tap { |v| v.instance_eval { def puts(*) end } }
125
+
126
+ commands = Pry.commands.dup
127
+ commands.output = options[:show_output] ? Pry.output : null_output
128
+ commands.target = Pry.binding_for(options[:context])
129
+
130
+ cmd = commands.commands[name]
131
+ if cmd
132
+ action = cmd[:action]
133
+ commands.instance_exec(*Shellwords.shellwords(arg_string), &action)
134
+ else
135
+ raise "No such command: #{name}"
136
+ end
137
+ end
138
+
97
139
  # Set all the configurable options back to their default values
98
140
  def self.reset_defaults
99
141
  @input = Readline
@@ -1,3 +1,3 @@
1
1
  class Pry
2
- VERSION = "0.5.2"
2
+ VERSION = "0.5.4"
3
3
  end
@@ -297,6 +297,43 @@ describe Pry do
297
297
  str_output2.string.should =~ /7/
298
298
  end
299
299
 
300
+ describe "Pry.run_command" do
301
+ before do
302
+ class RCTest
303
+ def a() end
304
+ B = 20
305
+ @x = 10
306
+ end
307
+ end
308
+
309
+ after do
310
+ Object.remove_const(:RCTest)
311
+ end
312
+
313
+ it "should execute command in the appropriate object context" do
314
+ result = Pry.run_command "ls", :context => RCTest
315
+ result.map(&:to_sym).should == [:@x]
316
+ end
317
+
318
+ it "should execute command with parameters in the appropriate object context" do
319
+ result = Pry.run_command "ls -M", :context => RCTest
320
+ result.map(&:to_sym).should == [:a]
321
+ end
322
+
323
+ it "should execute command and show output with :show_output => true flag" do
324
+ str = StringIO.new
325
+ Pry.output = str
326
+ result = Pry.run_command "ls -av", :context => RCTest, :show_output => true
327
+ str.string.should =~ /global variables/
328
+ Pry.output = $stdout
329
+ end
330
+
331
+ it "should execute command with multiple parameters" do
332
+ result = Pry.run_command "ls -c -M RCTest"
333
+ result.map(&:to_sym).should == [:a, :B]
334
+ end
335
+ end
336
+
300
337
  describe "commands" do
301
338
  it 'should set the commands default, and the default should be overridable' do
302
339
  class Command0 < Pry::CommandBase
metadata CHANGED
@@ -1,56 +1,80 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: pry
3
- version: !ruby/object:Gem::Version
4
- version: 0.5.2
3
+ version: !ruby/object:Gem::Version
4
+ hash: 3
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 5
9
+ - 4
10
+ version: 0.5.4
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - John Mair (banisterfiend)
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2011-02-17 00:00:00.000000000 +13:00
17
+
18
+ date: 2011-02-19 00:00:00 +13:00
13
19
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
16
22
  name: ruby_parser
17
- requirement: &17099076 !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
18
25
  none: false
19
- requirements:
20
- - - ! '>='
21
- - !ruby/object:Gem::Version
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 5
30
+ segments:
31
+ - 2
32
+ - 0
33
+ - 5
22
34
  version: 2.0.5
23
35
  type: :runtime
24
- prerelease: false
25
- version_requirements: *17099076
26
- - !ruby/object:Gem::Dependency
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
27
38
  name: method_source
28
- requirement: &17098800 !ruby/object:Gem::Requirement
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
29
41
  none: false
30
- requirements:
31
- - - ! '>='
32
- - !ruby/object:Gem::Version
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 23
46
+ segments:
47
+ - 0
48
+ - 2
49
+ - 0
33
50
  version: 0.2.0
34
51
  type: :runtime
35
- prerelease: false
36
- version_requirements: *17098800
37
- - !ruby/object:Gem::Dependency
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
38
54
  name: bacon
39
- requirement: &17098524 !ruby/object:Gem::Requirement
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
40
57
  none: false
41
- requirements:
42
- - - ! '>='
43
- - !ruby/object:Gem::Version
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 19
62
+ segments:
63
+ - 1
64
+ - 1
65
+ - 0
44
66
  version: 1.1.0
45
67
  type: :development
46
- prerelease: false
47
- version_requirements: *17098524
68
+ version_requirements: *id003
48
69
  description: attach an irb-like session to any object at runtime
49
70
  email: jrmair@gmail.com
50
71
  executables: []
72
+
51
73
  extensions: []
74
+
52
75
  extra_rdoc_files: []
53
- files:
76
+
77
+ files:
54
78
  - lib/pry/commands.rb
55
79
  - lib/pry/command_base.rb
56
80
  - lib/pry/completion.rb
@@ -82,26 +106,36 @@ files:
82
106
  has_rdoc: true
83
107
  homepage: http://banisterfiend.wordpress.com
84
108
  licenses: []
109
+
85
110
  post_install_message:
86
111
  rdoc_options: []
87
- require_paths:
112
+
113
+ require_paths:
88
114
  - lib
89
- required_ruby_version: !ruby/object:Gem::Requirement
115
+ required_ruby_version: !ruby/object:Gem::Requirement
90
116
  none: false
91
- requirements:
92
- - - ! '>='
93
- - !ruby/object:Gem::Version
94
- version: '0'
95
- required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 3
121
+ segments:
122
+ - 0
123
+ version: "0"
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
125
  none: false
97
- requirements:
98
- - - ! '>='
99
- - !ruby/object:Gem::Version
100
- version: '0'
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ hash: 3
130
+ segments:
131
+ - 0
132
+ version: "0"
101
133
  requirements: []
134
+
102
135
  rubyforge_project:
103
136
  rubygems_version: 1.5.2
104
137
  signing_key:
105
138
  specification_version: 3
106
139
  summary: attach an irb-like session to any object at runtime
107
140
  test_files: []
141
+