# Rails to C binding of libgio

**URL:** <https://www.ruby-forum.com/t/rails-to-c-binding-of-libgio/162662>\
**Category:** Rails\
**Created:** [March 12, 2009, 4:00pm UTC](https://www.ruby-forum.com/t/rails-to-c-binding-of-libgio/162662 "2009-03-12T16:00:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mvargo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8baadc/32.png) [@mvargo](https://www.ruby-forum.com/u/mvargo)\
**Post date:** [March 12, 2009, 4:00pm UTC](https://www.ruby-forum.com/t/rails-to-c-binding-of-libgio/162662/1 "2009-03-12T16:00:00Z")

</div>

I’m trying to use some functions from the Gnome GIO library to get  
extended file attributes. I’m doing this by writing a C extension  
using the wonderful directions in  
[http://www.rubycentral.com/pickaxe/ext\_ruby.html](http://www.rubycentral.com/pickaxe/ext_ruby.html).  
All that being said, I don’t really know Gnome or GTK+ at all, but I  
thought my little code would be OK.

So I have the library built (rubygio.so) and the code all executes  
fine. Problem is the gio api calls don’t return any info when I think  
they should. I’m guessing there is some initialization stuff I’m  
missing or maybe I have to have a window going or something. Any help  
would be appreciated.

The C code to get the attributes looks like this:

static VALUE rbgio\_load\_attributes(VALUE self)  
{  
VALUE arr = rb\_iv\_get(self, “@attribute\_array”);  
VALUE names = rb\_iv\_get(self, “@name\_array”);  
VALUE path = rb\_iv\_get(self, “@path”);

GFile \*ptr = g\_file\_new\_for\_path(STR2CSTR(path));  
GError _gerror = NULL;  
GFileInfo finfo = g\_file\_query\_info(ptr, "", 0, NULL, &gerror);  
if (finfo == NULL) {  
return INT2NUM(0);  
}  
char \*\*attributes = g\_file\_info\_list\_attributes(finfo, "_");  
int i = 0;  
while (attributes[i] != NULL) {  
rb\_ary\_push(names, rb\_str\_new2(attributes[i]));  
rb\_ary\_push(arr, rb\_str\_new2(g\_file\_info\_get\_attribute\_as\_string  
(finfo, attributes[i])));  
i++;  
}  
return INT2NUM(1)  
}

Again it all runs (i’ve run it in gdb and examined variables). The  
GFile _ptr is a real address, the GFileInfo finfo is a real thing and  
the char_ attributes is a valid list with no entries.

Any clues would be greatly appreciated. I will put the extension out  
there once completed.

Mike

---

<div class="post-metadata">

**Author:** ![mvargo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8baadc/32.png) [@mvargo](https://www.ruby-forum.com/u/mvargo)\
**Post date:** [March 12, 2009, 4:17pm UTC](https://www.ruby-forum.com/t/rails-to-c-binding-of-libgio/162662/2 "2009-03-12T16:17:00Z")

</div>

I see gets but no sets on those ivars you’re trying to manipulate. I  
wouldn’t assume that you’re editing the values in-place instead of a  
copy of what the ivars contained unless you’ve proven that’s what  
you’ve got (I think that sentence makes sense …).

Given that you’re saying the code doesn’t work, try adding the  
following 3 lines to the end of the method:

rb\_iv\_set(self, “@attributes\_array”, arr);  
rb\_iv\_set(self, “@name\_array”, names);  
rb\_iv\_set(self, “@path”, path);

Jason

---

<div class="post-metadata">

**Author:** ![mvargo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8baadc/32.png) [@mvargo](https://www.ruby-forum.com/u/mvargo)\
**Post date:** [March 12, 2009, 8:25pm UTC](https://www.ruby-forum.com/t/rails-to-c-binding-of-libgio/162662/3 "2009-03-12T20:25:03Z")

</div>

Sorry, I did not put the whole file in the post. Those are in the  
init module.

static VALUE rbgio\_init(VALUE self, VALUE path)  
{  
VALUE arr, names;

rb\_iv\_set(self, “@path”, path);  
arr = rb\_ary\_new();  
names = rb\_ary\_new();  
rb\_iv\_set(self, “@attribute\_array”, arr);  
rb\_iv\_set(self, “@name\_array”, names);  
return self;  
}

Thanks for looking. I’m starting to suspect I have to do something to  
initialize the library. I have a call to gdk\_init(argc, argv) in the  
module initialization, but maybe there is something more? Maybe I  
can’t use the library without creating a window? Not sure.

Mike
