Skip to content

Commit afb570f

Browse files
committed
Spec rb_keyword_given_p()
1 parent 88a0660 commit afb570f

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ static VALUE kernel_spec_rb_funcallv(VALUE self, VALUE obj, VALUE method, VALUE
326326
static VALUE kernel_spec_rb_funcallv_kw(VALUE self, VALUE obj, VALUE method, VALUE args) {
327327
return rb_funcallv_kw(obj, SYM2ID(method), RARRAY_LENINT(args), RARRAY_PTR(args), RB_PASS_KEYWORDS);
328328
}
329+
330+
static VALUE kernel_spec_rb_keyword_given_p(int argc, VALUE *args, VALUE self) {
331+
return rb_keyword_given_p() ? Qtrue : Qfalse;
332+
}
329333
#endif
330334

331335
static VALUE kernel_spec_rb_funcallv_public(VALUE self, VALUE obj, VALUE method) {
@@ -384,6 +388,7 @@ void Init_kernel_spec(void) {
384388
rb_define_method(cls, "rb_funcallv", kernel_spec_rb_funcallv, 3);
385389
#ifdef RUBY_VERSION_IS_3_0
386390
rb_define_method(cls, "rb_funcallv_kw", kernel_spec_rb_funcallv_kw, 3);
391+
rb_define_method(cls, "rb_keyword_given_p", kernel_spec_rb_keyword_given_p, -1);
387392
#endif
388393
rb_define_method(cls, "rb_funcallv_public", kernel_spec_rb_funcallv_public, 2);
389394
rb_define_method(cls, "rb_funcall_many_args", kernel_spec_rb_funcall_many_args, 2);

spec/ruby/optional/capi/kernel_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,23 @@ def m(*args, **kwargs)
605605
}.should raise_error(TypeError, 'no implicit conversion of Integer into Hash')
606606
end
607607
end
608+
609+
describe "rb_keyword_given_p" do
610+
it "returns whether keywords were given to the C extension method" do
611+
h = {a: 1}
612+
empty = {}
613+
@s.rb_keyword_given_p(a: 1).should == true
614+
@s.rb_keyword_given_p("foo" => "bar").should == true
615+
@s.rb_keyword_given_p(**h).should == true
616+
617+
@s.rb_keyword_given_p(h).should == false
618+
@s.rb_keyword_given_p().should == false
619+
@s.rb_keyword_given_p(**empty).should == false
620+
621+
@s.rb_funcallv_kw(@s, :rb_keyword_given_p, [{a: 1}]).should == true
622+
@s.rb_funcallv_kw(@s, :rb_keyword_given_p, [{}]).should == false
623+
end
624+
end
608625
end
609626

610627
describe "rb_funcallv_public" do

0 commit comments

Comments
 (0)