Forum: Ruby Returning File::Stat objects from a C extension

Posted by Andre Nathan (Guest)
on 2013-03-11 13:05
(Received via mailing list)
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
Posted by Hans Mackowiak (hanmac)
on 2013-03-11 13:18
i made you this ticket:
http://bugs.ruby-lang.org/issues/8050
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.