Returning File::Stat objects from a C extension

Hello

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 = {
“stat”,
{NULL, RUBY_TYPED_DEFAULT_FREE, stat_memsize,},
};
#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
objects.

What is the correct way to do this?

Thanks in advance,
Andre

i made you this ticket:
http://bugs.ruby-lang.org/issues/8050

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs