Skip to content
Permalink
Browse files

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(...).
  • Loading branch information
YorickPeterse committed May 13, 2015
1 parent a4673b9 commit 2642a30c57973987d2a7b7e85b3ce1e78100a31b
@@ -599,6 +599,7 @@
#define HAVE_RB_STRUCT_DEFINE 1
#define HAVE_RB_STRUCT_NEW 1
#define HAVE_RB_STRUCT_GETMEMBER 1
#define HAVE_RB_STRUCT_S_MEMBERS 1

/* Symbol */
#define HAVE_RB_ID2NAME 1
@@ -55,6 +55,12 @@ static VALUE struct_spec_rb_struct_new(VALUE self, VALUE klass,
}
#endif

#ifdef HAVE_RB_STRUCT_S_MEMBERS
static VALUE struct_spec_rb_struct_s_members(VALUE self, VALUE obj) {
return rb_struct_s_members(obj);
}
#endif

void Init_struct_spec() {
VALUE cls;
cls = rb_define_class("CApiStructSpecs", rb_cObject);
@@ -78,6 +84,10 @@ void Init_struct_spec() {
#ifdef HAVE_RB_STRUCT_NEW
rb_define_method(cls, "rb_struct_new", struct_spec_rb_struct_new, 4);
#endif

#ifdef HAVE_RB_STRUCT_S_MEMBERS
rb_define_method(cls, "rb_struct_s_members", struct_spec_rb_struct_s_members, 1);
#endif
}

#ifdef __cplusplus
@@ -126,4 +126,18 @@
i.c.should == 3
end
end

describe "rb_struct_s_members" do
it "returns the members of a Struct as an Array" do
struct = Struct.new(:foo, :bar)

@s.rb_struct_s_members(struct).should == [:foo, :bar]
end

it "returns the members of a Struct instance as an Array" do
struct = Struct.new(:foo, :bar).new

@s.rb_struct_s_members(struct).should == [:foo, :bar]
end
end
end
@@ -61,4 +61,8 @@ extern "C" {
VALUE rb_struct_aset(VALUE struct_handle, VALUE key, VALUE value) {
return rb_funcall(struct_handle, rb_intern("[]="), 2, key, value);
}

VALUE rb_struct_s_members(VALUE obj) {
return rb_funcall(obj, rb_intern("members"), 0);
}
}
@@ -1849,6 +1849,8 @@ struct RTypedData {
/** Sets the value of the key. */
VALUE rb_struct_aset(VALUE s, VALUE key, VALUE value);

VALUE rb_struct_s_members(VALUE obj);

/** Returns a String in locale encoding. */
VALUE rb_locale_str_new_cstr(const char* string);

0 comments on commit 2642a30

Please sign in to comment.
You can’t perform that action at this time.