1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

:takes_block now false by default, removed &block

* blocks can no longer be passed explicitly into commands via command "blah", "desc" |&block| (1.8 compatibility problems)
* blocks must be explicitly enabled for a command with the :takes_block => true option
This commit is contained in:
John Mair 2012-02-27 14:34:05 +13:00
parent ce920efd66
commit 8ace207020
3 changed files with 40 additions and 43 deletions

View file

@ -408,24 +408,7 @@ class Pry
# @param *String the arguments passed # @param *String the arguments passed
# @return Object the return value of the block # @return Object the return value of the block
def call(*args) def call(*args)
instance_exec(*correct_arg_arity(block.arity, args), &block)
# we need to define this method so we can pass a block in (not
# possible with `instance_exec`)
class << self; self; end.send(:define_method, :dummy, &block)
# Ruby 1.8 returns arity 1 for proc { |&block }, this is a bug
# and different to 1.9 behaviour (which returns 0). The
# following hack deals with this bug by retrying the method with
# no parameters if an ArgumentError is raised.
begin
dummy(*correct_arg_arity(block.arity, args), &command_block)
rescue ArgumentError => ex
if ex.message =~ /1 for 0/
dummy(&command_block)
else
raise
end
end
end end
def help def help
@ -463,7 +446,7 @@ class Pry
output.puts slop.help output.puts slop.help
void void
else else
process(*correct_arg_arity(method(:process).arity, args), &command_block) process(*correct_arg_arity(method(:process).arity, args))
end end
end end

View file

@ -337,7 +337,7 @@ class Pry
:shellwords => true, :shellwords => true,
:listing => name, :listing => name,
:use_prefix => true, :use_prefix => true,
:takes_block => true :takes_block => false
} }
end end

View file

@ -345,8 +345,8 @@ describe "Pry::Command" do
describe "block parameters" do describe "block parameters" do
before do before do
@context = Object.new @context = Object.new
@set.command "walking-spanish", "down the hall" do |&block| @set.command "walking-spanish", "down the hall", :takes_block => true do
inject_var(:@x, block.call, target) inject_var(:@x, command_block.call, target)
end end
@set.import Pry::Commands @set.import Pry::Commands
end end
@ -361,6 +361,22 @@ describe "Pry::Command" do
@context.instance_variable_get(:@x).should == :jesus @context.instance_variable_get(:@x).should == :jesus
end end
it 'should accept normal parameters along with block' do
@set.block_command "walking-spanish", "litella's been screeching for a blind pig.", :takes_block => true do |x, y|
inject_var(:@x, x, target)
inject_var(:@y, y, target)
inject_var(:@block_var, command_block.call, target)
end
redirect_pry_io(InputTester.new("walking-spanish john carl| { :jesus }",
"exit-all")) do
Pry.start @context, :commands => @set
end
@context.instance_variable_get(:@x).should == "john"
@context.instance_variable_get(:@y).should == "carl"
@context.instance_variable_get(:@block_var).should == :jesus
end
describe "single line blocks" do describe "single line blocks" do
it 'should accept blocks with do ; end' do it 'should accept blocks with do ; end' do
redirect_pry_io(InputTester.new("walking-spanish | do ; :jesus; end", redirect_pry_io(InputTester.new("walking-spanish | do ; :jesus; end",
@ -392,7 +408,7 @@ describe "Pry::Command" do
describe "arg_string" do describe "arg_string" do
it 'should remove block-related content from arg_string (with one normal arg)' do it 'should remove block-related content from arg_string (with one normal arg)' do
@set.block_command "walking-spanish", "down the hall" do |x, y| @set.block_command "walking-spanish", "down the hall", :takes_block => true do |x, y|
inject_var(:@arg_string, arg_string, target) inject_var(:@arg_string, arg_string, target)
inject_var(:@x, x, target) inject_var(:@x, x, target)
end end
@ -404,7 +420,7 @@ describe "Pry::Command" do
end end
it 'should remove block-related content from arg_string (with no normal args)' do it 'should remove block-related content from arg_string (with no normal args)' do
@set.block_command "walking-spanish", "down the hall" do @set.block_command "walking-spanish", "down the hall", :takes_block => true do
inject_var(:@arg_string, arg_string, target) inject_var(:@arg_string, arg_string, target)
end end
redirect_pry_io(InputTester.new("walking-spanish | { :jesus }", redirect_pry_io(InputTester.new("walking-spanish | { :jesus }",
@ -430,7 +446,7 @@ describe "Pry::Command" do
describe "args" do describe "args" do
describe "block_command" do describe "block_command" do
it "should remove block-related content from arguments" do it "should remove block-related content from arguments" do
@set.block_command "walking-spanish", "glass is full of sand" do |x, y| @set.block_command "walking-spanish", "glass is full of sand", :takes_block => true do |x, y|
inject_var(:@x, x, target) inject_var(:@x, x, target)
inject_var(:@y, y, target) inject_var(:@y, y, target)
end end
@ -458,7 +474,7 @@ describe "Pry::Command" do
describe "create_command" do describe "create_command" do
it "should remove block-related content from arguments" do it "should remove block-related content from arguments" do
@set.create_command "walking-spanish", "punk sanders carved one out of wood" do @set.create_command "walking-spanish", "punk sanders carved one out of wood", :takes_block => true do
def process(x, y) def process(x, y)
inject_var(:@x, x, target) inject_var(:@x, x, target)
inject_var(:@y, y, target) inject_var(:@y, y, target)
@ -493,12 +509,12 @@ describe "Pry::Command" do
describe "blocks can take parameters" do describe "blocks can take parameters" do
describe "{} style blocks" do describe "{} style blocks" do
it 'should accept multiple parameters' do it 'should accept multiple parameters' do
@set.block_command "walking-spanish", "down the hall" do |&block| @set.block_command "walking-spanish", "down the hall", :takes_block => true do
inject_var(:@x, block.call(1, 2), target) inject_var(:@x, command_block.call(1, 2), target)
end end
redirect_pry_io(InputTester.new("walking-spanish | { |x, y| [x, y] }", redirect_pry_io(InputTester.new("walking-spanish | { |x, y| [x, y] }",
"exit-all"), out = StringIO.new) do "exit-all")) do
Pry.start @context, :commands => @set Pry.start @context, :commands => @set
end end
@context.instance_variable_get(:@x).should == [1, 2] @context.instance_variable_get(:@x).should == [1, 2]
@ -507,16 +523,16 @@ describe "Pry::Command" do
describe "do/end style blocks" do describe "do/end style blocks" do
it 'should accept multiple parameters' do it 'should accept multiple parameters' do
@set.create_command "walking-spanish", "litella" do @set.create_command "walking-spanish", "litella", :takes_block => true do
def process(&block) def process
inject_var(:@x, block.call(1, 2), target) inject_var(:@x, command_block.call(1, 2), target)
end end
end end
redirect_pry_io(InputTester.new("walking-spanish | do |x, y|", redirect_pry_io(InputTester.new("walking-spanish | do |x, y|",
" [x, y]", " [x, y]",
"end", "end",
"exit-all"), out = StringIO.new) do "exit-all")) do
Pry.start @context, :commands => @set Pry.start @context, :commands => @set
end end
@context.instance_variable_get(:@x).should == [1, 2] @context.instance_variable_get(:@x).should == [1, 2]
@ -528,7 +544,7 @@ describe "Pry::Command" do
it 'should close over locals in the definition context' do it 'should close over locals in the definition context' do
redirect_pry_io(InputTester.new("var = :hello", redirect_pry_io(InputTester.new("var = :hello",
"walking-spanish | { var }", "walking-spanish | { var }",
"exit-all"), out = StringIO.new) do "exit-all")) do
Pry.start @context, :commands => @set Pry.start @context, :commands => @set
end end
@context.instance_variable_get(:@x).should == :hello @context.instance_variable_get(:@x).should == :hello
@ -538,35 +554,33 @@ describe "Pry::Command" do
describe "exposing block parameter" do describe "exposing block parameter" do
describe "block_command" do describe "block_command" do
it "should expose block in command_block method" do it "should expose block in command_block method" do
@set.block_command "walking-spanish", "glass full of sand" do @set.block_command "walking-spanish", "glass full of sand", :takes_block => true do
inject_var(:@x, command_block.call, target) inject_var(:@x, command_block.call, target)
end end
redirect_pry_io(InputTester.new("walking-spanish | { :jesus }", redirect_pry_io(InputTester.new("walking-spanish | { :jesus }",
"exit-all"), out = StringIO.new) do "exit-all")) do
Pry.start @context, :commands => @set Pry.start @context, :commands => @set
end end
@context.instance_variable_get(:@x).should == :jesus @context.instance_variable_get(:@x).should == :jesus
end end
# test for &block already completed many times in other sections
end end
describe "create_command" do describe "create_command" do
it "should expose &block in create_command's process method" do it "should NOT expose &block in create_command's process method" do
@set.create_command "walking-spanish", "down the hall" do @set.create_command "walking-spanish", "down the hall", :takes_block => true do
def process(&block) def process(&block)
inject_var(:@x, block.call, target) inject_var(:@x, block.call, target)
end end
end end
redirect_pry_io(InputTester.new("walking-spanish | { :jesus }", redirect_pry_io(InputTester.new("walking-spanish | { :jesus }",
"exit-all"), out = StringIO.new) do "exit-all")) do
Pry.start @context, :commands => @set Pry.start @context, :commands => @set
end end
@context.instance_variable_get(:@x).should == :jesus @context.instance_variable_get(:@x).should == nil
end end
it "should expose block in command_block method" do it "should expose block in command_block method" do
@set.create_command "walking-spanish", "homemade special" do @set.create_command "walking-spanish", "homemade special", :takes_block => true do
def process def process
inject_var(:@x, command_block.call, target) inject_var(:@x, command_block.call, target)
end end