pry 0.6.0 → 0.6.1

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.
data/CHANGELOG CHANGED
@@ -1,17 +1,25 @@
1
- 22/2/2010 version 0.5.8
1
+ 26/2/2011 version 0.6.1
2
+ * !@ command alias for exit_all
3
+ * `cd /` for breaking out to pry top level (jump-to 0)
4
+ * made `-e` option work in a more effective way for `pry` command line invocation
5
+ * exit and exit-all commands now accept a parameter, this parameter becomes the return value of repl()
6
+ * `command` method from CommandBase now accepts a :keep_retval arg that determines if command value is returned to pry session or just `nil` (`nil` was old behaviour)
7
+ * tests for new :keep_retval and exit-all/exit behaviour; :keep_retval will remain undocumented.
8
+
9
+ 22/2/2011 version 0.5.8
2
10
  * Added -c (context) option to show-doc, show-methods and eval-file
3
11
  * Fixed up ordering issue of -c and -r parameters to command line pry
4
12
 
5
- 21/2/2010 version 0.5.7
13
+ 21/2/2011 version 0.5.7
6
14
  * Added pry executable, auto-loads .pryrc in user's home directory, if it
7
15
  exists.
8
16
 
9
- 19/2/2010 version 0.5.5
17
+ 19/2/2011 version 0.5.5
10
18
  * Added Pry.run_command
11
19
  * More useful error messages
12
20
  * Easter eggs (game and cohen-poem)
13
21
 
14
- 17/2/2010 version 0.5.0
22
+ 17/2/2011 version 0.5.0
15
23
  * Use clipped version of Pry.view() for large objects
16
24
  * Exit Pry session on ^d
17
25
  * Use Shellwords for breaking up parameters to pry commands
@@ -27,16 +35,16 @@
27
35
  * Get rid of show_idoc and show_imethod
28
36
  * Add special eval-file command that evals target file in current context
29
37
 
30
- 27/1/2010 version 0.4.5
38
+ 27/1/2011 version 0.4.5
31
39
  * fixed show_method (though fragile as it references __binding_impl__
32
40
  directly, making a name change to that method difficult
33
- 27/1/2010 version 0.4.4
41
+ 27/1/2011 version 0.4.4
34
42
  * oops, added examples/ directory
35
- 26/1/2010 version 0.4.3
43
+ 26/1/2011 version 0.4.3
36
44
  * added alias_command and desc methods to Pry::CommandBase
37
45
  * changed behaviour of ls_methods and ls_imethods to return sorted lists
38
46
  of methods
39
- 23/1/2010 version 0.4.1
47
+ 23/1/2011 version 0.4.1
40
48
  * made it so a 'def meth;end' in an object Pry session defines singleton
41
49
  methods, not methods on the class (except in the case of
42
50
  immediates)
@@ -44,7 +52,7 @@
44
52
  * storing wiki in a nested git repo, as github wiki pages have their own
45
53
  repo
46
54
  * added more tests for new method definition behaviour
47
- x21/1/2010 version 0.4.0
55
+ 21/1/2011 version 0.4.0
48
56
  * added command API
49
57
  * added many new commands, i.e ls_methods and friends
50
58
  * modified other commands
@@ -13,7 +13,8 @@ session to your code (with Pry) instead of bringing your code to a
13
13
  REPL session (as with IRB).
14
14
 
15
15
  It is not based on the IRB codebase, and implements some unique REPL
16
- commands such as `show-method`, `show-doc`, `ls` and `cd`
16
+ commands such as `show-method`, `show-doc`, `ls` and `cd` (type `help`
17
+ to get a full list).
17
18
 
18
19
  Pry is also fairly flexible and allows significant user
19
20
  [customization](http://rdoc.info/github/banister/pry/master/file/wiki/Customizing-pry.md). It
data/bin/pry CHANGED
@@ -61,10 +61,9 @@ load rcpath if File.exists?(rcpath) && options[:loadrc]
61
61
  # create the actual context
62
62
  context = Pry.binding_for(eval(options[:context_string]))
63
63
 
64
- # execute line of code, if provided with -e option
64
+ # run code passed with `-e`, if there is any.
65
65
  if options[:code]
66
- result = context.eval(options[:code])
67
- puts "=> #{Pry.view(result)}"
66
+ Pry.new(:input => StringIO.new(options[:code]), :print => proc {}).rep(context)
68
67
  end
69
68
 
70
69
  # start the session
@@ -33,11 +33,19 @@ class Pry
33
33
  # # Good afternoon John!
34
34
  # # pry(main)> help greet
35
35
  # # Greet somebody
36
- def command(names, description="No description.", &block)
36
+ def command(names, description="No description.", options={}, &block)
37
+ options = {
38
+ :keep_retval => false
39
+ }.merge!(options)
40
+
37
41
  @commands ||= {}
38
42
 
39
43
  Array(names).each do |name|
40
- commands[name] = { :description => description, :action => block }
44
+ commands[name] = {
45
+ :description => description,
46
+ :action => block,
47
+ :keep_retval => options[:keep_retval]
48
+ }
41
49
  end
42
50
  end
43
51
 
@@ -1,6 +1,9 @@
1
1
  direc = File.dirname(__FILE__)
2
- require "#{direc}/command_base"
2
+
3
3
  require "optparse"
4
+ require "method_source"
5
+ require "#{direc}/command_base"
6
+ require "#{direc}/pry_instance"
4
7
 
5
8
  class Pry
6
9
 
@@ -72,10 +75,13 @@ class Pry
72
75
  output.puts "Pry version: #{Pry::VERSION} on Ruby #{RUBY_VERSION}."
73
76
  end
74
77
 
75
- command "exit-all", "End all nested Pry sessions." do
76
- throw(:breakout, 0)
78
+ command "exit-all", "End all nested Pry sessions. Accepts optional return value. Aliases: !@" do
79
+ str = opts[:val].split.drop(1).join(' ')
80
+ throw(:breakout, [0, target.eval(str)])
77
81
  end
78
82
 
83
+ alias_command "!@", "exit-all", ""
84
+
79
85
  command "ls", "Show the list of vars in the current scope. Type `ls --help` for more info." do |*args|
80
86
  options = {}
81
87
 
@@ -210,7 +216,7 @@ Shows local and instance variables by default.
210
216
  if options[:v]
211
217
 
212
218
  # verbose
213
- info.each.sort_by { |k, v| v.last }.each do |k, v|
219
+ info.sort_by { |k, v| v.last }.each do |k, v|
214
220
  if !v.first.empty?
215
221
  output.puts "#{k}:\n--"
216
222
  output.puts Pry.view(v.first)
@@ -218,7 +224,7 @@ Shows local and instance variables by default.
218
224
  end
219
225
  end
220
226
 
221
- # plain
227
+ # plain
222
228
  else
223
229
  list = info.values.sort_by { |v| v.last }.map { |v| v.first }.inject(&:+)
224
230
  output.puts Pry.view(list)
@@ -232,7 +238,9 @@ Shows local and instance variables by default.
232
238
  next
233
239
  end
234
240
 
235
- output.puts File.read(file_name)
241
+ contents = File.read(file_name)
242
+ output.puts contents
243
+ contents
236
244
  end
237
245
 
238
246
  command "eval-file", "Eval a Ruby script. Type `eval-file --help` for more info." do |*args|
@@ -290,13 +298,20 @@ e.g: eval-file -c self "hello.rb"
290
298
 
291
299
  alias_command "inspect", "cat", ""
292
300
 
293
- command "cd", "Start a Pry session on VAR (use `cd ..` to go back)" do |obj|
301
+ command "cd", "Start a Pry session on VAR (use `cd ..` to go back and `cd /` to return to Pry top-level)",
302
+ :keep_retval => true do |obj|
294
303
  if !obj
295
304
  output.puts "Must provide an object."
296
305
  next
297
306
  end
298
307
 
299
308
  throw(:breakout, opts[:nesting].level) if obj == ".."
309
+
310
+ if obj == "/"
311
+ throw(:breakout, 1) if opts[:nesting].level > 0
312
+ next
313
+ end
314
+
300
315
  target.eval("#{obj}.pry")
301
316
  end
302
317
 
@@ -452,8 +467,9 @@ e.g: show-method hello_method
452
467
  end
453
468
  end
454
469
 
455
- command "exit", "End the current Pry session. Aliases: quit, back" do
456
- throw(:breakout, opts[:nesting].level)
470
+ command "exit", "End the current Pry session. Accepts optional return value. Aliases: quit, back" do
471
+ str = opts[:val].split.drop(1).join(' ')
472
+ throw(:breakout, [opts[:nesting].level, target.eval(str)])
457
473
  end
458
474
 
459
475
  alias_command "quit", "exit", ""
@@ -515,7 +531,7 @@ on these enormous landscapes,
515
531
  that if you turn your head
516
532
  they are lost for hours.
517
533
  -- Leonard Cohen
518
- }
534
+ }
519
535
  output.puts text
520
536
  text
521
537
  end
@@ -1,5 +1,13 @@
1
+ direc = File.dirname(__FILE__)
2
+
1
3
  require 'readline'
2
4
  require 'shellwords'
5
+ require "#{direc}/prompts"
6
+ require "#{direc}/hooks"
7
+ require "#{direc}/print"
8
+ require "#{direc}/commands"
9
+ require "#{direc}/core_extensions"
10
+ require "#{direc}/pry_instance"
3
11
 
4
12
  # @author John Mair (banisterfiend)
5
13
  class Pry
@@ -57,6 +65,8 @@ class Pry
57
65
  # @return [Array<Proc>] The array of Procs to be used for the
58
66
  # prompts by default by all Pry instances.
59
67
  attr_accessor :prompt
68
+
69
+ attr_accessor :cmd_ret_value
60
70
  end
61
71
 
62
72
  # Start a Pry REPL.
@@ -86,7 +86,7 @@ class Pry
86
86
  target.eval("_pry_ = Pry.active_instance")
87
87
  target.eval("_ = Pry.last_result")
88
88
 
89
- break_level = catch(:breakout) do
89
+ break_data = catch(:breakout) do
90
90
  nesting.push [nesting.size, target_self, self]
91
91
  loop do
92
92
  rep(target)
@@ -97,11 +97,18 @@ class Pry
97
97
 
98
98
  exec_hook :after_session, output, target_self
99
99
 
100
+ # If break_data is an array, then the last element is the return value
101
+ break_level, return_value = Array(break_data)
102
+
100
103
  # keep throwing until we reach the desired nesting level
101
104
  if nesting_level != break_level
102
- throw :breakout, break_level
105
+ throw :breakout, break_data
103
106
  end
104
107
 
108
+ # if one was provided, return the return value
109
+ return return_value if return_value
110
+
111
+ # otherwise return the target_self
105
112
  target_self
106
113
  end
107
114
 
@@ -170,8 +177,13 @@ class Pry
170
177
 
171
178
  val.chomp!
172
179
 
173
- process_commands(val, eval_string, target)
174
- eval_string << "#{val}\n"
180
+ Pry.cmd_ret_value = process_commands(val, eval_string, target)
181
+
182
+ if Pry.cmd_ret_value
183
+ eval_string << "Pry.cmd_ret_value\n"
184
+ else
185
+ eval_string << "#{val}\n"
186
+ end
175
187
 
176
188
  break eval_string if valid_expression?(eval_string)
177
189
  end
@@ -190,43 +202,49 @@ class Pry
190
202
  def val.clear() replace("") end
191
203
  def eval_string.clear() replace("") end
192
204
 
193
- pattern, data = commands.commands.find do |name, data|
205
+ pattern, cmd_data = commands.commands.find do |name, cmd_data|
194
206
  /^#{name}(?!\S)(?:\s+(.+))?/ =~ val
195
207
  end
196
208
 
197
- if pattern
198
- args_string = $1
199
- args = args_string ? Shellwords.shellwords(args_string) : []
200
- action = data[:action]
201
-
202
- options = {
203
- :val => val,
204
- :eval_string => eval_string,
205
- :nesting => nesting,
206
- :commands => commands.commands
207
- }
208
-
209
- # set some useful methods to be used by the action blocks
210
- commands.opts = options
211
- commands.target = target
212
- commands.output = output
213
-
214
- case action.arity <=> 0
215
- when -1
216
-
217
- # Use instance_exec() to make the `opts` method, etc available
218
- commands.instance_exec(*args, &action)
219
- when 1, 0
220
-
221
- # ensure that we get the right number of parameters
222
- # since 1.8.7 complains about incorrect arity (1.9.2
223
- # doesn't care)
224
- args_with_corrected_arity = args.values_at *0..(action.arity - 1)
225
- commands.instance_exec(*args_with_corrected_arity, &action)
226
- end
227
-
228
- val.clear
209
+ # no command was matched, so return to caller
210
+ return if !pattern
211
+
212
+ args_string = $1
213
+ args = args_string ? Shellwords.shellwords(args_string) : []
214
+ action = cmd_data[:action]
215
+ keep_retval = cmd_data[:keep_retval]
216
+
217
+ options = {
218
+ :val => val,
219
+ :eval_string => eval_string,
220
+ :nesting => nesting,
221
+ :commands => commands.commands
222
+ }
223
+
224
+ # set some useful methods to be used by the action blocks
225
+ commands.opts = options
226
+ commands.target = target
227
+ commands.output = output
228
+
229
+ case action.arity <=> 0
230
+ when -1
231
+
232
+ # Use instance_exec() to make the `opts` method, etc available
233
+ ret_value = commands.instance_exec(*args, &action)
234
+ when 1, 0
235
+
236
+ # ensure that we get the right number of parameters
237
+ # since 1.8.7 complains about incorrect arity (1.9.2
238
+ # doesn't care)
239
+ args_with_corrected_arity = args.values_at *0..(action.arity - 1)
240
+ ret_value = commands.instance_exec(*args_with_corrected_arity, &action)
229
241
  end
242
+
243
+ # a command was processed so we can now clear the input string
244
+ val.clear
245
+
246
+ # return value of block only if :keep_retval is true
247
+ ret_value if keep_retval
230
248
  end
231
249
 
232
250
  # Returns the next line of input to be used by the pry instance.
@@ -1,3 +1,3 @@
1
1
  class Pry
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.1"
3
3
  end
@@ -335,6 +335,33 @@ describe Pry do
335
335
  end
336
336
 
337
337
  describe "commands" do
338
+ it 'should define a command that keeps its return value' do
339
+ class Command68 < Pry::CommandBase
340
+ command "hello", "", :keep_retval => true do
341
+ :kept_hello
342
+ end
343
+ end
344
+ str_output = StringIO.new
345
+ Pry.new(:input => StringIO.new("hello"), :output => str_output, :commands => Command68).rep
346
+ str_output.string.should =~ /:kept_hello/
347
+
348
+ Object.remove_const(:Command68)
349
+ end
350
+
351
+ it 'should define a command that does NOT keep its return value' do
352
+ class Command68 < Pry::CommandBase
353
+ command "hello", "", :keep_retval => false do
354
+ :kept_hello
355
+ end
356
+ end
357
+ str_output = StringIO.new
358
+ Pry.new(:input => StringIO.new("hello"), :output => str_output, :commands => Command68).rep
359
+ (str_output.string =~ /:kept_hello/).should == nil
360
+
361
+ Object.remove_const(:Command68)
362
+ end
363
+
364
+
338
365
  it 'should set the commands default, and the default should be overridable' do
339
366
  class Command0 < Pry::CommandBase
340
367
  command "hello" do
@@ -537,6 +564,28 @@ describe Pry do
537
564
  Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
538
565
  str_output.string.should == "test\n"
539
566
  end
567
+
568
+ describe "pry return values" do
569
+ it 'should return the target object' do
570
+ Pry.start(self, :input => StringIO.new("exit"), :output => Pry::NullOutput).should == self
571
+ end
572
+
573
+ it 'should return the parameter given to exit' do
574
+ Pry.start(self, :input => StringIO.new("exit 10"), :output => Pry::NullOutput).should == 10
575
+ end
576
+
577
+ it 'should return the parameter (multi word string) given to exit' do
578
+ Pry.start(self, :input => StringIO.new("exit \"john mair\""), :output => Pry::NullOutput).should == "john mair"
579
+ end
580
+
581
+ it 'should return the parameter (function call) given to exit' do
582
+ Pry.start(self, :input => StringIO.new("exit 'abc'.reverse"), :output => Pry::NullOutput).should == 'cba'
583
+ end
584
+
585
+ it 'should return the parameter (self) given to exit' do
586
+ Pry.start("carl", :input => StringIO.new("exit self"), :output => Pry::NullOutput).should == "carl"
587
+ end
588
+ end
540
589
 
541
590
  describe "prompts" do
542
591
  it 'should set the prompt default, and the default should be overridable (single prompt)' do
metadata CHANGED
@@ -1,80 +1,57 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: pry
3
- version: !ruby/object:Gem::Version
4
- hash: 7
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.1
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 6
9
- - 0
10
- version: 0.6.0
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - John Mair (banisterfiend)
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-02-22 00:00:00 +13:00
12
+ date: 2011-02-26 00:00:00.000000000 +13:00
19
13
  default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
22
16
  name: ruby_parser
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: &17331060 !ruby/object:Gem::Requirement
25
18
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 5
30
- segments:
31
- - 2
32
- - 0
33
- - 5
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
34
22
  version: 2.0.5
35
23
  type: :runtime
36
- version_requirements: *id001
37
- - !ruby/object:Gem::Dependency
38
- name: method_source
39
24
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
25
+ version_requirements: *17331060
26
+ - !ruby/object:Gem::Dependency
27
+ name: method_source
28
+ requirement: &17330712 !ruby/object:Gem::Requirement
41
29
  none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- hash: 23
46
- segments:
47
- - 0
48
- - 2
49
- - 0
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
50
33
  version: 0.2.0
51
34
  type: :runtime
52
- version_requirements: *id002
53
- - !ruby/object:Gem::Dependency
54
- name: bacon
55
35
  prerelease: false
56
- requirement: &id003 !ruby/object:Gem::Requirement
36
+ version_requirements: *17330712
37
+ - !ruby/object:Gem::Dependency
38
+ name: bacon
39
+ requirement: &17330376 !ruby/object:Gem::Requirement
57
40
  none: false
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- hash: 19
62
- segments:
63
- - 1
64
- - 1
65
- - 0
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
66
44
  version: 1.1.0
67
45
  type: :development
68
- version_requirements: *id003
46
+ prerelease: false
47
+ version_requirements: *17330376
69
48
  description: attach an irb-like session to any object at runtime
70
49
  email: jrmair@gmail.com
71
- executables:
50
+ executables:
72
51
  - pry
73
52
  extensions: []
74
-
75
53
  extra_rdoc_files: []
76
-
77
- files:
54
+ files:
78
55
  - lib/pry/commands.rb
79
56
  - lib/pry/command_base.rb
80
57
  - lib/pry/completion.rb
@@ -107,36 +84,26 @@ files:
107
84
  has_rdoc: true
108
85
  homepage: http://banisterfiend.wordpress.com
109
86
  licenses: []
110
-
111
87
  post_install_message:
112
88
  rdoc_options: []
113
-
114
- require_paths:
89
+ require_paths:
115
90
  - lib
116
- required_ruby_version: !ruby/object:Gem::Requirement
91
+ required_ruby_version: !ruby/object:Gem::Requirement
117
92
  none: false
118
- requirements:
119
- - - ">="
120
- - !ruby/object:Gem::Version
121
- hash: 3
122
- segments:
123
- - 0
124
- version: "0"
125
- required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
98
  none: false
127
- requirements:
128
- - - ">="
129
- - !ruby/object:Gem::Version
130
- hash: 3
131
- segments:
132
- - 0
133
- version: "0"
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
134
103
  requirements: []
135
-
136
104
  rubyforge_project:
137
105
  rubygems_version: 1.5.2
138
106
  signing_key:
139
107
  specification_version: 3
140
108
  summary: attach an irb-like session to any object at runtime
141
109
  test_files: []
142
-