Skip to content

Commit 2642a30

Browse files
author
Yorick Peterse
committedMay 13, 2015
Added rb_struct_s_members()
This function simply returns the members of a Struct, although it segfaults on MRI when given an instance of the return value of Struct.new(...).
1 parent a4673b9 commit 2642a30

File tree

5 files changed

+31
-0
lines changed

5 files changed

+31
-0
lines changed
 

Diff for: ‎spec/ruby/optional/capi/ext/rubyspec.h

+1
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@
599599
#define HAVE_RB_STRUCT_DEFINE 1
600600
#define HAVE_RB_STRUCT_NEW 1
601601
#define HAVE_RB_STRUCT_GETMEMBER 1
602+
#define HAVE_RB_STRUCT_S_MEMBERS 1
602603

603604
/* Symbol */
604605
#define HAVE_RB_ID2NAME 1

Diff for: ‎spec/ruby/optional/capi/ext/struct_spec.c

+10
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ static VALUE struct_spec_rb_struct_new(VALUE self, VALUE klass,
5555
}
5656
#endif
5757

58+
#ifdef HAVE_RB_STRUCT_S_MEMBERS
59+
static VALUE struct_spec_rb_struct_s_members(VALUE self, VALUE obj) {
60+
return rb_struct_s_members(obj);
61+
}
62+
#endif
63+
5864
void Init_struct_spec() {
5965
VALUE cls;
6066
cls = rb_define_class("CApiStructSpecs", rb_cObject);
@@ -78,6 +84,10 @@ void Init_struct_spec() {
7884
#ifdef HAVE_RB_STRUCT_NEW
7985
rb_define_method(cls, "rb_struct_new", struct_spec_rb_struct_new, 4);
8086
#endif
87+
88+
#ifdef HAVE_RB_STRUCT_S_MEMBERS
89+
rb_define_method(cls, "rb_struct_s_members", struct_spec_rb_struct_s_members, 1);
90+
#endif
8191
}
8292

8393
#ifdef __cplusplus

Diff for: ‎spec/ruby/optional/capi/struct_spec.rb

+14
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,18 @@
126126
i.c.should == 3
127127
end
128128
end
129+
130+
describe "rb_struct_s_members" do
131+
it "returns the members of a Struct as an Array" do
132+
struct = Struct.new(:foo, :bar)
133+
134+
@s.rb_struct_s_members(struct).should == [:foo, :bar]
135+
end
136+
137+
it "returns the members of a Struct instance as an Array" do
138+
struct = Struct.new(:foo, :bar).new
139+
140+
@s.rb_struct_s_members(struct).should == [:foo, :bar]
141+
end
142+
end
129143
end

Diff for: ‎vm/capi/struct.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,8 @@ extern "C" {
6161
VALUE rb_struct_aset(VALUE struct_handle, VALUE key, VALUE value) {
6262
return rb_funcall(struct_handle, rb_intern("[]="), 2, key, value);
6363
}
64+
65+
VALUE rb_struct_s_members(VALUE obj) {
66+
return rb_funcall(obj, rb_intern("members"), 0);
67+
}
6468
}

Diff for: ‎vm/include/capi/ruby/ruby.h

+2
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,8 @@ struct RTypedData {
18491849
/** Sets the value of the key. */
18501850
VALUE rb_struct_aset(VALUE s, VALUE key, VALUE value);
18511851

1852+
VALUE rb_struct_s_members(VALUE obj);
1853+
18521854
/** Returns a String in locale encoding. */
18531855
VALUE rb_locale_str_new_cstr(const char* string);
18541856

0 commit comments

Comments
 (0)
Please sign in to comment.