Returning File::Stat objects from a C extension


I’m trying to find out how to return File::Stat objects from a C
extension. In 1.8 I can do it like this:

#define WRAP_STAT(stp) Data_Wrap_Struct(rb_cStat, NULL, free, stp)

static VALUE
stat_new(struct stat *st)
struct stat *stp = ALLOC(struct stat);
*stp = *st;
return WRAP_STAT(stp);

Starting in 1.9 it seems the correct way would be to do it like this:

static size_t
stat_memsize(const void *p)
return p ? sizeof(struct stat) : 0;
static const rb_data_type_t stat_data_type = {
#define WRAP_STAT(stp)
TypedData_Wrap_Struct(rb_cStat, &stat_data_type, stp)

However this leads to the following error:

#<File::Stat/usr/lib/ruby/1.9.1/pp.rb:405:in `dev’: wrong argument type
stat (expected stat) (TypeError)

An analogous error also happens when I try to do the same for Dir

What is the correct way to do this?

Thanks in advance,

i made you this ticket: