Skip to content

Commit dbc7695

Browse files
committed
Add C API rb_proc_call_with_block function
1 parent aafc401 commit dbc7695

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

spec/ruby/optional/capi/ext/proc_spec.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ VALUE proc_spec_rb_proc_call(VALUE self, VALUE prc, VALUE args) {
7676
return rb_proc_call(prc, args);
7777
}
7878

79+
VALUE proc_spec_rb_proc_call_with_block(VALUE self, VALUE prc, VALUE args, VALUE block) {
80+
return rb_proc_call_with_block(prc, RARRAY_LENINT(args), RARRAY_PTR(args), block);
81+
}
82+
7983
VALUE proc_spec_rb_obj_is_proc(VALUE self, VALUE prc) {
8084
return rb_obj_is_proc(prc);
8185
}
@@ -123,6 +127,7 @@ void Init_proc_spec(void) {
123127
rb_define_method(cls, "rb_proc_new_block_given_p", proc_spec_rb_proc_new_block_given_p, 0);
124128
rb_define_method(cls, "rb_proc_arity", proc_spec_rb_proc_arity, 1);
125129
rb_define_method(cls, "rb_proc_call", proc_spec_rb_proc_call, 2);
130+
rb_define_method(cls, "rb_proc_call_with_block", proc_spec_rb_proc_call_with_block, 3);
126131
rb_define_method(cls, "rb_Proc_new", proc_spec_rb_Proc_new, 1);
127132
rb_define_method(cls, "rb_obj_is_proc", proc_spec_rb_obj_is_proc, 1);
128133
}

spec/ruby/optional/capi/proc_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@
8282
end
8383
end
8484

85+
describe "rb_proc_call_with_block" do
86+
it "calls the Proc and passes arguments and a block" do
87+
prc = Proc.new { |a, b, &block| block.call(a * b) }
88+
@p.rb_proc_call_with_block(prc, [6, 7], proc { |n| n * 2 }).should == 6 * 7 * 2
89+
end
90+
91+
it "calls the Proc and passes arguments when a block is nil" do
92+
prc = Proc.new { |a, b| a * b }
93+
@p.rb_proc_call_with_block(prc, [6, 7], nil).should == 6 * 7
94+
end
95+
end
96+
8597
describe "rb_obj_is_proc" do
8698
it "returns true for Proc" do
8799
prc = Proc.new {|a,b| a * b }

src/main/c/cext/proc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ VALUE rb_proc_call(VALUE self, VALUE args) {
1919
return RUBY_CEXT_INVOKE("rb_proc_call", self, args);
2020
}
2121

22+
VALUE rb_proc_call_with_block(VALUE recv, int argc, const VALUE *argv, VALUE proc) {
23+
return rb_funcall_with_block(recv, rb_intern("call"), argc, argv, proc);
24+
}
25+
2226
int rb_proc_arity(VALUE self) {
2327
return polyglot_as_i32(RUBY_INVOKE_NO_WRAP(self, "arity"));
2428
}

0 commit comments

Comments
 (0)