# C ext to Ruby interaction between a Module and a Class

**URL:** <https://www.ruby-forum.com/t/c-ext-to-ruby-interaction-between-a-module-and-a-class/111166>\
**Category:** Ruby\
**Created:** [August 3, 2007, 1:05pm UTC](https://www.ruby-forum.com/t/c-ext-to-ruby-interaction-between-a-module-and-a-class/111166 "2007-08-03T13:05:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 3, 2007, 1:05pm UTC](https://www.ruby-forum.com/t/c-ext-to-ruby-interaction-between-a-module-and-a-class/111166/1 "2007-08-03T13:05:50Z")

</div>

I’m presently writing C extension to Ruby (under and for Mac OS X).

i do have a Ruby Module called “ROSXUtils” and i want to return a  
class coming from another C ext to Ruby. The class being RAliasFile.

i don’t know exactly how to do that.

for the time being i’ve put the class RAliasFile under a sub-directory  
“lib” of my ext/ (of the Module ROSXUtils) :

ext/  
extconf.rb  
lib/  
extconf.rb  
dll\_p.c  
dll\_p.h  
raliasfile.bundle  
raliasfile.c  
rosxutils.bundle  
rosxutils.c  
UnixUtils.c  
UnixUtils.h

BUT, for the time being in order to use, from C, the RAliasFile class,  
i’m obliged to use :

rb\_eval\_string(“require ‘/absolute/path/to/raliasfile’”);

then to return an instance of this class :

VALUE alias = rb\_eval\_string(“RAliasFile.new(arg)”);

this works but isn’t elegant ???

what could be the best way to instantiate a class being a C ext to  
Ruby from a module being also a C ext to Ruby ???

also is my dir hierarchy shown above the best way ?

i’d like better having the class RAliasFile included in the  
rmosxutils.bundle…

any comment appreciated 😉

Yvon

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 3, 2007, 5:11pm UTC](https://www.ruby-forum.com/t/c-ext-to-ruby-interaction-between-a-module-and-a-class/111166/2 "2007-08-03T17:11:23Z")

</div>

Hi,

At Fri, 3 Aug 2007 20:05:01 +0900,  
unbewust wrote in [ruby-talk:263179]:

> for the time being i’ve put the class RAliasFile under a sub-directory  
> “lib” of my ext/ (of the Module ROSXUtils) :

Since Makefile generated by mkmf.rb will install script files  
under lib, it’s not good idea to use that name for another  
extension.

> BUT, for the time being in order to use, from C, the RAliasFile class,  
> i’m obliged to use :
> 
> rb\_eval\_string(“require ‘/absolute/path/to/raliasfile’”);
> 
> then to return an instance of this class :
> 
> VALUE alias = rb\_eval\_string(“RAliasFile.new(arg)”);

rb\_require(“raliasfile”);  
alias = rb\_class\_new\_instance(1, &arg, rb\_path2class(“RAliasFile”));

> i’d like better having the class RAliasFile included in the  
> rmosxutils.bundle…

A static library instead of a shared object will be created by  
setting $static true in extconf.rb.

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 3, 2007, 5:19pm UTC](https://www.ruby-forum.com/t/c-ext-to-ruby-interaction-between-a-module-and-a-class/111166/3 "2007-08-03T17:19:24Z")

</div>

On 8/3/07, unbewust [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> BUT, for the time being in order to use, from C, the RAliasFile class,  
> i’m obliged to use :
> 
> rb\_eval\_string(“require ‘/absolute/path/to/raliasfile’”);

rb\_require( “relative/path/to/raliasfile” );

My preference is relative paths. Eventually you’ll want to release  
this as a gem to other developers, and they might not have the exact  
some directory structure that you do.

> then to return an instance of this class :
> 
> VALUE alias = rb\_eval\_string(“RAliasFile.new(arg)”);

VALUE alias = rb\_class\_new\_instance( 1, args, RAliasFile );

Where 1 is the number of arguments in the args array, args is the  
array of arguments (as a VALUE\*), and RAliasFile is a reference to the  
class you would like to create.

> also is my dir hierarchy shown above the best way ?

Unless you need separate modules, I would flatten your directory tree  
– i.e. consolidate all the files in your ext/lib folder up into the  
ext folder.

> any comment appreciated 😉

Hope these were helpful suggestions.

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 3, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/c-ext-to-ruby-interaction-between-a-module-and-a-class/111166/4 "2007-08-03T18:45:34Z")

</div>

On 3 août, 17:17, “Tim P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Unless you need separate modules, I would flatten your directory tree  
> – i.e. consolidate all the files in your ext/lib folder up into the  
> ext folder.

no, no, because they are closely dependant…

in fact i want to make something like FileUtils (included in ruby now)  
BUT for the files on Mac OS X.

that’s to say being able to create alias file, alias record, put files  
in the trash and letting the file manager and also the alias manager  
knows where everything is.

this would allow to “untrash” a file and/or a directory…

thanks a lot !

Yvon

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 4, 2007, 11:11am UTC](https://www.ruby-forum.com/t/c-ext-to-ruby-interaction-between-a-module-and-a-class/111166/5 "2007-08-04T11:11:00Z")

</div>

On 3 août, 17:17, “Tim P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> VALUE alias = rb\_class\_new\_instance( 1, args, RAliasFile );  
> – i.e. consolidate all the files in your ext/lib folder up into the  
> ext folder.
> 
> > any comment appreciated 😉
> 
> Hope these were helpful suggestions.
> 
> Blessings,  
> TwP

OK with relative path this is OK now, the prob arroses because my ruby  
file to test was in another folder.

BUT i still have prob with “rb\_require(‘raf/raliasfile’);” :  
//make =\> rosxutils.c:105: warning: passing argument 1 of rb\_require  
makes pointer from integer without a cast  
// run =\> ./sample.rb:124: [BUG] Segmentation fault

may be it’s only the form of the argument in cause here ie ‘raf/  
raliasfile’ (see make result).

remarks here my set-up is still with a sub-dir (renamed raf instead of  
lib) :

ext/  
extconf.rb  
Makefile  
raf/  
dll\_path.c  
dll\_path.h  
extconf.rb  
Makefile  
raliasfile.bundle  
RAliasFile.c  
RAliasFile.o  
rosxutils.bundle  
rosxutils.c  
rosxutils.o  
sample.rb  
UnixUtils.c  
UnixUtils.h  
UnixUtils.o

remarks also if i make use of rb\_eval\_string(‘relative path’)  
everything wents OK.

the prob with absolute path cames from my sample.rb file (then ruby  
side) which wasn’t, at that time in the dir ext…

NOW if I put everything in the same diretory and adding $static = true  
in extconf.rb i get :

./sample.rb:124:in `mkAlias’: undefined class/module RAliasFile  
(ArgumentError)

i don’t have any file to load except rosxutils

then is there a way to know, afterwards what’s in a bundle file ???

thanks for your help !

obviously when a first version OK i’ll release that as a gem…

Yvon

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 4, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/c-ext-to-ruby-interaction-between-a-module-and-a-class/111166/6 "2007-08-04T15:16:24Z")

</div>

Hi,

At Sat, 4 Aug 2007 18:10:02 +0900,  
unbewust wrote in [ruby-talk:263323]:

> BUT i still have prob with “rb\_require(‘raf/raliasfile’);” :  
> //make =\> rosxutils.c:105: warning: passing argument 1 of rb\_require  
> makes pointer from integer without a cast

String literal in C must be enclosed with double-quotes, not  
single-quotes. Multiple characters inside single-quotes would  
be an OSX specific extension, perhaps inherited from old MacOS.

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 4, 2007, 3:40pm UTC](https://www.ruby-forum.com/t/c-ext-to-ruby-interaction-between-a-module-and-a-class/111166/7 "2007-08-04T15:40:25Z")

</div>

Nobuyoshi N. wrote:

> unbewust wrote:

> > BUT i still have prob with “rb\_require(‘raf/raliasfile’);” :  
> > //make =\> rosxutils.c:105: warning: passing argument 1 of rb\_require  
> > makes pointer from integer without a cast
> 
> String literal in C must be enclosed with double-quotes, not  
> single-quotes. Multiple characters inside single-quotes would  
> be an OSX specific extension, perhaps inherited from old MacOS.

It’s not specifically an extension - it’s a multiple byte character. The  
literal ‘riffraff’ would form a long-long (on some architures),  
containing  
those ASCII codes all packed into its bits.

That’s why the compiler ignores any trailing characters that don’t fit  
into  
its integer type, then issues a bizarre complain’t about converting an  
integer to a pointer. “” decays to a pointer.

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 5, 2007, 12:56am UTC](https://www.ruby-forum.com/t/c-ext-to-ruby-interaction-between-a-module-and-a-class/111166/8 "2007-08-05T00:56:08Z")

</div>

In message [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
unbewust [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes

> On 3 août, 17:17, “Tim P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > On 8/3/07, unbewust [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> > …
> 
> BUT i still have prob with “rb\_require(‘raf/raliasfile’);” :  
> //make =\> rosxutils.c:105: warning: passing argument 1 of rb\_require  
> makes pointer from integer without a cast  
> // run =\> ./sample.rb:124: [BUG] Segmentation fault
> 
> may be it’s only the form of the argument in cause here ie ‘raf/  
> raliasfile’ (see make result).

Would

“rb\_require("raf/raliasfile");”

help?

(I.e replace the single quotes w/ escaped double quotes, within the  
double quoted string literal.)

HTH

Alec R.

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 4, 2007, 9:49pm UTC](https://www.ruby-forum.com/t/c-ext-to-ruby-interaction-between-a-module-and-a-class/111166/9 "2007-08-04T21:49:55Z")

</div>

Hi,

At Sat, 4 Aug 2007 22:39:22 +0900,  
Phlip wrote in [ruby-talk:263340]:

> > > BUT i still have prob with “rb\_require(‘raf/raliasfile’);” :  
> > > //make =\> rosxutils.c:105: warning: passing argument 1 of rb\_require  
> > > makes pointer from integer without a cast
> > 
> > String literal in C must be enclosed with double-quotes, not  
> > single-quotes. Multiple characters inside single-quotes would  
> > be an OSX specific extension, perhaps inherited from old MacOS.
> 
> It’s not specifically an extension - it’s a multiple byte character. The

I meant it would not be defined by C89, C99 nor K&R. In C++,  
it is called as “multicharacter literal”, but an  
implementation-defined feature.
