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:
parent
ce920efd66
commit
8ace207020
3 changed files with 40 additions and 43 deletions
|
@ -408,24 +408,7 @@ class Pry
|
|||
# @param *String the arguments passed
|
||||
# @return Object the return value of the block
|
||||
def call(*args)
|
||||
|
||||
# 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
|
||||
instance_exec(*correct_arg_arity(block.arity, args), &block)
|
||||
end
|
||||
|
||||
def help
|
||||
|
@ -463,7 +446,7 @@ class Pry
|
|||
output.puts slop.help
|
||||
void
|
||||
else
|
||||
process(*correct_arg_arity(method(:process).arity, args), &command_block)
|
||||
process(*correct_arg_arity(method(:process).arity, args))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -337,7 +337,7 @@ class Pry
|
|||
:shellwords => true,
|
||||
:listing => name,
|
||||
:use_prefix => true,
|
||||
:takes_block => true
|
||||
:takes_block => false
|
||||
}
|
||||
end
|
||||
|
||||
|
|
|
@ -345,8 +345,8 @@ describe "Pry::Command" do
|
|||
describe "block parameters" do
|
||||
before do
|
||||
@context = Object.new
|
||||
@set.command "walking-spanish", "down the hall" do |&block|
|
||||
inject_var(:@x, block.call, target)
|
||||
@set.command "walking-spanish", "down the hall", :takes_block => true do
|
||||
inject_var(:@x, command_block.call, target)
|
||||
end
|
||||
@set.import Pry::Commands
|
||||
end
|
||||
|
@ -361,6 +361,22 @@ describe "Pry::Command" do
|
|||
@context.instance_variable_get(:@x).should == :jesus
|
||||
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
|
||||
it 'should accept blocks with do ; end' do
|
||||
redirect_pry_io(InputTester.new("walking-spanish | do ; :jesus; end",
|
||||
|
@ -392,7 +408,7 @@ describe "Pry::Command" do
|
|||
|
||||
describe "arg_string" 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(:@x, x, target)
|
||||
end
|
||||
|
@ -404,7 +420,7 @@ describe "Pry::Command" do
|
|||
end
|
||||
|
||||
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)
|
||||
end
|
||||
redirect_pry_io(InputTester.new("walking-spanish | { :jesus }",
|
||||
|
@ -430,7 +446,7 @@ describe "Pry::Command" do
|
|||
describe "args" do
|
||||
describe "block_command" 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(:@y, y, target)
|
||||
end
|
||||
|
@ -458,7 +474,7 @@ describe "Pry::Command" do
|
|||
|
||||
describe "create_command" 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)
|
||||
inject_var(:@x, x, target)
|
||||
inject_var(:@y, y, target)
|
||||
|
@ -493,12 +509,12 @@ describe "Pry::Command" do
|
|||
describe "blocks can take parameters" do
|
||||
describe "{} style blocks" do
|
||||
it 'should accept multiple parameters' do
|
||||
@set.block_command "walking-spanish", "down the hall" do |&block|
|
||||
inject_var(:@x, block.call(1, 2), target)
|
||||
@set.block_command "walking-spanish", "down the hall", :takes_block => true do
|
||||
inject_var(:@x, command_block.call(1, 2), target)
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
@context.instance_variable_get(:@x).should == [1, 2]
|
||||
|
@ -507,16 +523,16 @@ describe "Pry::Command" do
|
|||
|
||||
describe "do/end style blocks" do
|
||||
it 'should accept multiple parameters' do
|
||||
@set.create_command "walking-spanish", "litella" do
|
||||
def process(&block)
|
||||
inject_var(:@x, block.call(1, 2), target)
|
||||
@set.create_command "walking-spanish", "litella", :takes_block => true do
|
||||
def process
|
||||
inject_var(:@x, command_block.call(1, 2), target)
|
||||
end
|
||||
end
|
||||
|
||||
redirect_pry_io(InputTester.new("walking-spanish | do |x, y|",
|
||||
" [x, y]",
|
||||
"end",
|
||||
"exit-all"), out = StringIO.new) do
|
||||
"exit-all")) do
|
||||
Pry.start @context, :commands => @set
|
||||
end
|
||||
@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
|
||||
redirect_pry_io(InputTester.new("var = :hello",
|
||||
"walking-spanish | { var }",
|
||||
"exit-all"), out = StringIO.new) do
|
||||
"exit-all")) do
|
||||
Pry.start @context, :commands => @set
|
||||
end
|
||||
@context.instance_variable_get(:@x).should == :hello
|
||||
|
@ -538,35 +554,33 @@ describe "Pry::Command" do
|
|||
describe "exposing block parameter" do
|
||||
describe "block_command" 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)
|
||||
end
|
||||
redirect_pry_io(InputTester.new("walking-spanish | { :jesus }",
|
||||
"exit-all"), out = StringIO.new) do
|
||||
"exit-all")) do
|
||||
Pry.start @context, :commands => @set
|
||||
end
|
||||
@context.instance_variable_get(:@x).should == :jesus
|
||||
end
|
||||
|
||||
# test for &block already completed many times in other sections
|
||||
end
|
||||
|
||||
describe "create_command" do
|
||||
it "should expose &block in create_command's process method" do
|
||||
@set.create_command "walking-spanish", "down the hall" do
|
||||
it "should NOT expose &block in create_command's process method" do
|
||||
@set.create_command "walking-spanish", "down the hall", :takes_block => true do
|
||||
def process(&block)
|
||||
inject_var(:@x, block.call, target)
|
||||
end
|
||||
end
|
||||
redirect_pry_io(InputTester.new("walking-spanish | { :jesus }",
|
||||
"exit-all"), out = StringIO.new) do
|
||||
"exit-all")) do
|
||||
Pry.start @context, :commands => @set
|
||||
end
|
||||
@context.instance_variable_get(:@x).should == :jesus
|
||||
@context.instance_variable_get(:@x).should == nil
|
||||
end
|
||||
|
||||
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
|
||||
inject_var(:@x, command_block.call, target)
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue