proc.c: proc without block
* proc.c (proc_new): promoted lambda/proc/Proc.new with no block in a method called with a block to a warning/error. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66772 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
ec336fb40e
commit
9f1fb0a17f
5
NEWS
5
NEWS
@ -17,6 +17,11 @@ sufficient information, see the ChangeLog file or Redmine
|
|||||||
* Method reference operator, <code>.:</code> is introduced as an
|
* Method reference operator, <code>.:</code> is introduced as an
|
||||||
experimental feature. [Feature #12125] [Feature #13581]
|
experimental feature. [Feature #12125] [Feature #13581]
|
||||||
|
|
||||||
|
* Proc.new and proc with no block in a method called with a block is warned
|
||||||
|
now.
|
||||||
|
|
||||||
|
* lambda with no block in a method called with a block errs.
|
||||||
|
|
||||||
=== Core classes updates (outstanding ones only)
|
=== Core classes updates (outstanding ones only)
|
||||||
|
|
||||||
=== Stdlib updates (outstanding ones only)
|
=== Stdlib updates (outstanding ones only)
|
||||||
|
@ -514,12 +514,11 @@ class Matrix
|
|||||||
# # => prints the numbers 1 to 4
|
# # => prints the numbers 1 to 4
|
||||||
# Matrix[ [1,2], [3,4] ].each(:strict_lower).to_a # => [3]
|
# Matrix[ [1,2], [3,4] ].each(:strict_lower).to_a # => [3]
|
||||||
#
|
#
|
||||||
def each(which = :all) # :yield: e
|
def each(which = :all, &block) # :yield: e
|
||||||
return to_enum :each, which unless block_given?
|
return to_enum :each, which unless block_given?
|
||||||
last = column_count - 1
|
last = column_count - 1
|
||||||
case which
|
case which
|
||||||
when :all
|
when :all
|
||||||
block = Proc.new
|
|
||||||
@rows.each do |row|
|
@rows.each do |row|
|
||||||
row.each(&block)
|
row.each(&block)
|
||||||
end
|
end
|
||||||
|
@ -283,9 +283,9 @@ class Prime
|
|||||||
end
|
end
|
||||||
|
|
||||||
# see +Enumerator+#with_index.
|
# see +Enumerator+#with_index.
|
||||||
def with_index(offset = 0)
|
def with_index(offset = 0, &block)
|
||||||
return enum_for(:with_index, offset) { Float::INFINITY } unless block_given?
|
return enum_for(:with_index, offset) { Float::INFINITY } unless block
|
||||||
return each_with_index(&proc) if offset == 0
|
return each_with_index(&block) if offset == 0
|
||||||
|
|
||||||
each do |prime|
|
each do |prime|
|
||||||
yield prime, offset
|
yield prime, offset
|
||||||
|
3
proc.c
3
proc.c
@ -707,6 +707,9 @@ proc_new(VALUE klass, int8_t is_lambda)
|
|||||||
|
|
||||||
if ((block_handler = rb_vm_frame_block_handler(cfp)) != VM_BLOCK_HANDLER_NONE) {
|
if ((block_handler = rb_vm_frame_block_handler(cfp)) != VM_BLOCK_HANDLER_NONE) {
|
||||||
if (is_lambda) {
|
if (is_lambda) {
|
||||||
|
rb_raise(rb_eArgError, proc_without_block);
|
||||||
|
}
|
||||||
|
else {
|
||||||
rb_warn(proc_without_block);
|
rb_warn(proc_without_block);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,14 +36,28 @@ describe "Kernel.proc" do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "Kernel#proc" do
|
describe "Kernel#proc" do
|
||||||
it "uses the implicit block from an enclosing method" do
|
ruby_version_is ""..."2.7" do
|
||||||
def some_method
|
it "uses the implicit block from an enclosing method" do
|
||||||
proc
|
def some_method
|
||||||
|
proc
|
||||||
|
end
|
||||||
|
|
||||||
|
prc = some_method { "hello" }
|
||||||
|
|
||||||
|
prc.call.should == "hello"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
prc = some_method { "hello" }
|
ruby_version_is "2.7" do
|
||||||
|
it "can be created when called with no block" do
|
||||||
|
def some_method
|
||||||
|
proc
|
||||||
|
end
|
||||||
|
|
||||||
prc.call.should == "hello"
|
-> {
|
||||||
|
some_method { "hello" }
|
||||||
|
}.should complain(/tried to create Proc object without a block/)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "needs to be reviewed for spec completeness"
|
it "needs to be reviewed for spec completeness"
|
||||||
|
@ -20,22 +20,39 @@ describe "Proc as a block pass argument" do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "Proc as an implicit block pass argument" do
|
ruby_version_is ""..."2.7" do
|
||||||
def revivify
|
describe "Proc as an implicit block pass argument" do
|
||||||
Proc.new
|
def revivify
|
||||||
end
|
Proc.new
|
||||||
|
end
|
||||||
|
|
||||||
it "remains the same object if re-vivified by the target method" do
|
it "remains the same object if re-vivified by the target method" do
|
||||||
p = Proc.new {}
|
p = Proc.new {}
|
||||||
p2 = revivify(&p)
|
p2 = revivify(&p)
|
||||||
p.should equal p2
|
p.should equal p2
|
||||||
p.should == p2
|
p.should == p2
|
||||||
end
|
end
|
||||||
|
|
||||||
it "remains the same object if reconstructed with Proc.new" do
|
it "remains the same object if reconstructed with Proc.new" do
|
||||||
p = Proc.new {}
|
p = Proc.new {}
|
||||||
p2 = Proc.new(&p)
|
p2 = Proc.new(&p)
|
||||||
p.should equal p2
|
p.should equal p2
|
||||||
p.should == p2
|
p.should == p2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ruby_version_is "2.7" do
|
||||||
|
describe "Proc called with no block" do
|
||||||
|
def revivify
|
||||||
|
Proc.new
|
||||||
|
end
|
||||||
|
|
||||||
|
it "raises ArgumentError when called with no block" do
|
||||||
|
p = Proc.new {}
|
||||||
|
-> {
|
||||||
|
revivify(&p)
|
||||||
|
}.should
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -95,16 +95,18 @@ describe "Proc.new with an associated block" do
|
|||||||
obj.second.should == 2
|
obj.second.should == 2
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns a new Proc instance from the block passed to the containing method" do
|
ruby_version_is ""..."2.7" do
|
||||||
prc = ProcSpecs.new_proc_in_method { "hello" }
|
it "returns a new Proc instance from the block passed to the containing method" do
|
||||||
prc.should be_an_instance_of(Proc)
|
prc = ProcSpecs.new_proc_in_method { "hello" }
|
||||||
prc.call.should == "hello"
|
prc.should be_an_instance_of(Proc)
|
||||||
end
|
prc.call.should == "hello"
|
||||||
|
end
|
||||||
|
|
||||||
it "returns a new Proc instance from the block passed to the containing method" do
|
it "returns a new Proc instance from the block passed to the containing method" do
|
||||||
prc = ProcSpecs.new_proc_subclass_in_method { "hello" }
|
prc = ProcSpecs.new_proc_subclass_in_method { "hello" }
|
||||||
prc.should be_an_instance_of(ProcSpecs::ProcSubclass)
|
prc.should be_an_instance_of(ProcSpecs::ProcSubclass)
|
||||||
prc.call.should == "hello"
|
prc.call.should == "hello"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -178,13 +180,36 @@ describe "Proc.new without a block" do
|
|||||||
lambda { ProcSpecs.new_proc_subclass_in_method }.should raise_error(ArgumentError)
|
lambda { ProcSpecs.new_proc_subclass_in_method }.should raise_error(ArgumentError)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "uses the implicit block from an enclosing method" do
|
ruby_version_is ""..."2.7" do
|
||||||
def some_method
|
it "uses the implicit block from an enclosing method" do
|
||||||
Proc.new
|
def some_method
|
||||||
|
Proc.new
|
||||||
|
end
|
||||||
|
|
||||||
|
prc = some_method { "hello" }
|
||||||
|
|
||||||
|
prc.call.should == "hello"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ruby_version_is "2.7" do
|
||||||
|
it "can be created if invoked from within a method with a block" do
|
||||||
|
lambda { ProcSpecs.new_proc_in_method { "hello" } }.should complain(/tried to create Proc object without a block/)
|
||||||
end
|
end
|
||||||
|
|
||||||
prc = some_method { "hello" }
|
it "can be created if invoked on a subclass from within a method with a block" do
|
||||||
|
lambda { ProcSpecs.new_proc_subclass_in_method { "hello" } }.should complain(/tried to create Proc object without a block/)
|
||||||
|
end
|
||||||
|
|
||||||
prc.call.should == "hello"
|
|
||||||
|
it "can be create when called with no block" do
|
||||||
|
def some_method
|
||||||
|
Proc.new
|
||||||
|
end
|
||||||
|
|
||||||
|
-> {
|
||||||
|
some_method { "hello" }
|
||||||
|
}.should complain(/tried to create Proc object without a block/)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -310,14 +310,25 @@ describe "A lambda expression 'lambda { ... }'" do
|
|||||||
def meth; lambda; end
|
def meth; lambda; end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "can be created" do
|
ruby_version_is ""..."2.7" do
|
||||||
implicit_lambda = nil
|
it "can be created" do
|
||||||
-> {
|
implicit_lambda = nil
|
||||||
implicit_lambda = meth { 1 }
|
-> {
|
||||||
}.should complain(/tried to create Proc object without a block/)
|
implicit_lambda = meth { 1 }
|
||||||
|
}.should complain(/tried to create Proc object without a block/)
|
||||||
|
|
||||||
implicit_lambda.lambda?.should be_true
|
implicit_lambda.lambda?.should be_true
|
||||||
implicit_lambda.call.should == 1
|
implicit_lambda.call.should == 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ruby_version_is "2.7" do
|
||||||
|
it "raises ArgumentError" do
|
||||||
|
implicit_lambda = nil
|
||||||
|
-> {
|
||||||
|
meth { 1 }
|
||||||
|
}.should raise_error(ArgumentError, /tried to create Proc object without a block/)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -69,16 +69,18 @@ describe "C-API when calling Proc.new from a C function" do
|
|||||||
# For example: C -> Ruby <- C -> Ruby means a C function called into Ruby
|
# For example: C -> Ruby <- C -> Ruby means a C function called into Ruby
|
||||||
# code which returned to C, then C called into Ruby code again.
|
# code which returned to C, then C called into Ruby code again.
|
||||||
|
|
||||||
# Ruby -> C -> rb_funcall(Proc.new)
|
ruby_version_is ""..."2.7" do
|
||||||
it "returns the Proc passed by the Ruby code calling the C function" do
|
# Ruby -> C -> rb_funcall(Proc.new)
|
||||||
prc = @p.rb_Proc_new(0) { :called }
|
it "returns the Proc passed by the Ruby code calling the C function" do
|
||||||
prc.call.should == :called
|
prc = @p.rb_Proc_new(0) { :called }
|
||||||
end
|
prc.call.should == :called
|
||||||
|
end
|
||||||
|
|
||||||
# Ruby -> C -> Ruby <- C -> rb_funcall(Proc.new)
|
# Ruby -> C -> Ruby <- C -> rb_funcall(Proc.new)
|
||||||
it "returns the Proc passed to the Ruby method when the C function calls other Ruby methods before calling Proc.new" do
|
it "returns the Proc passed to the Ruby method when the C function calls other Ruby methods before calling Proc.new" do
|
||||||
prc = @p.rb_Proc_new(1) { :called }
|
prc = @p.rb_Proc_new(1) { :called }
|
||||||
prc.call.should == :called
|
prc.call.should == :called
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Ruby -> C -> Ruby -> Proc.new
|
# Ruby -> C -> Ruby -> Proc.new
|
||||||
@ -93,16 +95,18 @@ describe "C-API when calling Proc.new from a C function" do
|
|||||||
lambda { @p.rb_Proc_new(3) { :called } }.should raise_error(ArgumentError)
|
lambda { @p.rb_Proc_new(3) { :called } }.should raise_error(ArgumentError)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Ruby -> C -> Ruby -> C (with new block) -> rb_funcall(Proc.new)
|
ruby_version_is ""..."2.7" do
|
||||||
it "returns the most recent Proc passed when the Ruby method called the C function" do
|
# Ruby -> C -> Ruby -> C (with new block) -> rb_funcall(Proc.new)
|
||||||
prc = @p.rb_Proc_new(4) { :called }
|
it "returns the most recent Proc passed when the Ruby method called the C function" do
|
||||||
prc.call.should == :calling_with_block
|
prc = @p.rb_Proc_new(4) { :called }
|
||||||
end
|
prc.call.should == :calling_with_block
|
||||||
|
end
|
||||||
|
|
||||||
# Ruby -> C -> Ruby -> C (with new block) <- Ruby <- C -> # rb_funcall(Proc.new)
|
# Ruby -> C -> Ruby -> C (with new block) <- Ruby <- C -> # rb_funcall(Proc.new)
|
||||||
it "returns the Proc passed from the original Ruby call to the C function" do
|
it "returns the Proc passed from the original Ruby call to the C function" do
|
||||||
prc = @p.rb_Proc_new(5) { :called }
|
prc = @p.rb_Proc_new(5) { :called }
|
||||||
prc.call.should == :called
|
prc.call.should == :called
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Ruby -> C -> Ruby -> block_given?
|
# Ruby -> C -> Ruby -> block_given?
|
||||||
|
@ -424,14 +424,14 @@ class TestProc < Test::Unit::TestCase
|
|||||||
1.times { b = lambda }
|
1.times { b = lambda }
|
||||||
b
|
b
|
||||||
end
|
end
|
||||||
assert_equal(:foo, o.foo { :foo }.call)
|
assert_raise(ArgumentError) {o.foo { :foo }.call}
|
||||||
|
|
||||||
def o.foo(&b)
|
def o.foo(&b)
|
||||||
b = nil
|
b = nil
|
||||||
1.times { b = lambda }
|
1.times { b = lambda }
|
||||||
b
|
b
|
||||||
end
|
end
|
||||||
assert_equal(:foo, o.foo { :foo }.call)
|
assert_raise(ArgumentError) {o.foo { :foo }.call}
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_arity2
|
def test_arity2
|
||||||
|
Loading…
x
Reference in New Issue
Block a user