# Help making a ruby module

**URL:** <https://www.ruby-forum.com/t/help-making-a-ruby-module/98493>\
**Category:** Ruby\
**Created:** [July 15, 2006, 1:04am UTC](https://www.ruby-forum.com/t/help-making-a-ruby-module/98493 "2006-07-15T01:04:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_McGovern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ccd318/32.png) [@Ryan\_McGovern](https://www.ruby-forum.com/u/Ryan_McGovern)\
**Post date:** [July 15, 2006, 1:04am UTC](https://www.ruby-forum.com/t/help-making-a-ruby-module/98493/1 "2006-07-15T01:04:05Z")

</div>

I am working on making a ruby module to wrapper the xsan quota fctl  
calls.  
I have C code that will get the quota information, but using the picaxe  
book i dont quite understand how to make a wrapper.  
Essecally i need 2 functions, a GetQuota function that would return a  
Ruby struct with the information and a SetQuota function  
the GetQuota would take only a name(“mcgoverp”) and a type (“U”), the  
SetQuota function would take a hard limit soft limit and time out as  
well as a name and a type.

This is my current trial

#include “ruby.h”  
#include “./extapi.h”  
#include \<errno.h\>  
#include \<fcntl.h\>  
#include \<pwd.h\>

static VALUE cXsanQuota;  
static void free\_quota(void \*p)  
{

}

static VALUE quota\_alloc(VALUE klass){

}

static VALUE quota\_initialize(VALUE self)  
{  
return self;  
}  
static VALUE get\_quota(VALUE self,VALUE user,VALUE system)  
{  
const char\* in\_file = “/Home/”;  
int in\_fd = open(in\_file, O\_RDONLY); //open existing file  
GetQuotaReqReply\_t test;  
GetQuotaReq\_t request;  
request.gq\_type=‘U’;  
strcpy(request.gq\_quotaname,t.pw\_name);  
test.req=request;  
int res=fcntl(in\_fd,F\_GETQUOTA,&test);  
[//test.response](https://test.response) has struct  
//how do i return that?  
}  
static VALUE set\_quota(VALUE self,VALUE user, VALUE hl, VALUE sl, VALUE  
to)  
{  
SetQuotaReq\_t setquota;  
char \*str;  
str=RSTRING(user)-\>ptr;  
strcpy(setquota.sq\_quotaname,str);  
setquota.sq\_type=“U”;  
setquota.sq\_timelimit=FIX2INT(to);  
setquota.sq\_hardlimit=NUM2ULONG(hl);  
setquota.sq\_softlimit=NUM2ULONG(sl);  
res=fcntl(in\_fd,F\_SETQUOTA,&setquota);  
return res;  
}  
void Init\_XsanQuota()  
{  
cXsanQuota=rb\_define\_class(“XsanQuota”,rb\_cObject);  
rb\_define\_alloc\_func(cXsanQuota,quota\_alloc);  
rb\_define\_method(cXsanQuota,“initialize”, quota\_initialize,1);  
rb\_define\_method(cXsanQuota,“getQuota”, quota\_initialize,3);  
rb\_define\_method(cXsanQuota,“setQuota”, quota\_initialize,5);  
}

---

<div class="post-metadata">

**Author:** ![Ryan\_McGovern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ccd318/32.png) [@Ryan\_McGovern](https://www.ruby-forum.com/u/Ryan_McGovern)\
**Post date:** [July 15, 2006, 12:26pm UTC](https://www.ruby-forum.com/t/help-making-a-ruby-module/98493/2 "2006-07-15T12:26:20Z")

</div>

> > > > > “R” == Ryan McGovern [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

R\> int res=fcntl(in\_fd,F\_GETQUOTA,&test);  
R\> [//test.response](https://test.response) has struct  
R\> //how do i return that?

See ext/etc/etc.c in the ruby distribution to see how create and return  
a  
ruby struct.

R\> }  
R\> static VALUE set\_quota(VALUE self,VALUE user, VALUE hl, VALUE sl,  
VALUE to)  
R\> {  
R\> SetQuotaReq\_t setquota;  
R\> char \*str;  
R\> str=RSTRING(user)-\>ptr;

Never do this : what if `user’ is a Float ?

Use StringValuePtr()

R\> strcpy(setquota.sq\_quotaname,str);

R\> void Init\_XsanQuota()

best to call it `Init\_xsanquota’

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Ryan\_McGovern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ccd318/32.png) [@Ryan\_McGovern](https://www.ruby-forum.com/u/Ryan_McGovern)\
**Post date:** [July 15, 2006, 3:18pm UTC](https://www.ruby-forum.com/t/help-making-a-ruby-module/98493/3 "2006-07-15T15:18:05Z")

</div>

Hi,

At Sat, 15 Jul 2006 08:01:11 +0900,  
Ryan McGovern wrote in [ruby-talk:202039]:

> Essecally i need 2 functions, a GetQuota function that would return a  
> Ruby struct with the information and a SetQuota function  
> the GetQuota would take only a name(“mcgoverp”) and a type (“U”), the  
> SetQuota function would take a hard limit soft limit and time out as  
> well as a name and a type.
> 
> This is my current trial

Seems module functions better.

> void Init\_XsanQuota()  
> {  
> cXsanQuota=rb\_define\_module(“XsanQuota”);  
> rb\_define\_module\_function(cXsanQuota,“getQuota”,  
> quota\_initialize,3);  
> rb\_define\_module\_function(cXsanQuota,“setQuota”,  
> quota\_initialize,5);  
> }

free\_quota(), quota\_alloc() and quota\_initialize() aren’t needed.

---

<div class="post-metadata">

**Author:** ![Ryan\_McGovern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ccd318/32.png) [@Ryan\_McGovern](https://www.ruby-forum.com/u/Ryan_McGovern)\
**Post date:** [July 24, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/help-making-a-ruby-module/98493/4 "2006-07-24T19:40:14Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Seems module functions better.

Thanks for the help, i also figured it out using SWIG, though it’s just  
a temporary solution
