[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Init_xxx(with arg ??) in C for ruby ext ???

pere.noel

8/8/2006 4:44:00 PM

hey all,

new to C, i'm writing a ruby ext, i wonder if we could pass an arg to
Init_xxx(type * arg) ???

this is to have a constructor in ruby like that :

trick=MyClass.new("my arg")

and if not does exists a workaround ???

--
une bévue
13 Answers

pere.noel

8/8/2006 5:21:00 PM

0

Une bévue <pere.noel@laponie.com.invalid> wrote:

> ew to C, i'm writing a ruby ext, i wonder if we could pass an arg to
> Init_xxx(type * arg) ???
>
> this is to have a constructor in ruby like that :
>
> trick=MyClass.new("my arg")
>
> and if not does exists a workaround ???

it is :

rb_define_singleton_method(rb_cMyClass, "new", cd_new, 1);

--
une bévue

Eric Hodel

8/8/2006 5:23:00 PM

0

On Aug 8, 2006, at 9:45 AM, Une bévue wrote:

> hey all,
>
> new to C, i'm writing a ruby ext, i wonder if we could pass an arg to
> Init_xxx(type * arg) ???
>
> this is to have a constructor in ruby like that :
>
> trick=MyClass.new("my arg")
>
> and if not does exists a workaround ???

The Init_ method initializes the shared library, it is not the
constructor.

Write your #initialize to take arguments and MyClass.new "my arg"
will work.

--
Eric Hodel - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...



Eric Hodel

8/8/2006 7:13:00 PM

0

On Aug 8, 2006, at 10:25 AM, Une bévue wrote:

> Une bévue <pere.noel@laponie.com.invalid> wrote:
>
>> ew to C, i'm writing a ruby ext, i wonder if we could pass an arg to
>> Init_xxx(type * arg) ???
>>
>> this is to have a constructor in ruby like that :
>>
>> trick=MyClass.new("my arg")
>>
>> and if not does exists a workaround ???
>
> it is :
>
> rb_define_singleton_method(rb_cMyClass, "new", cd_new, 1);

No. MyClass.new is inherited from Class.

rb_define_method(rb_cMyClass, "new", cd_initialize, 1);

It works "just like" regular ruby.

--
Eric Hodel - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...



Eric Hodel

8/8/2006 10:21:00 PM

0

On Aug 8, 2006, at 12:13 PM, Eric Hodel wrote:

> On Aug 8, 2006, at 10:25 AM, Une bévue wrote:
>
>> Une bévue <pere.noel@laponie.com.invalid> wrote:
>>
>>> ew to C, i'm writing a ruby ext, i wonder if we could pass an arg to
>>> Init_xxx(type * arg) ???
>>>
>>> this is to have a constructor in ruby like that :
>>>
>>> trick=MyClass.new("my arg")
>>>
>>> and if not does exists a workaround ???
>>
>> it is :
>>
>> rb_define_singleton_method(rb_cMyClass, "new", cd_new, 1);
>
> No. MyClass.new is inherited from Class.
>
> rb_define_method(rb_cMyClass, "new", cd_initialize, 1);

Oops.

rb_define_method(rb_cMyClass, "initialize", cd_initialize, 1);

--
Eric Hodel - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...



Yvon Thoraval

8/8/2006 10:56:00 PM

0


Le 9 août 06 à 00:21, Eric Hodel a écrit :

> On Aug 8, 2006, at 12:13 PM, Eric Hodel wrote:
>
[snip]
> Oops.
>
> rb_define_method(rb_cMyClass, "initialize", cd_initialize, 1);


ok thanks, i get it, now i've an init like that :

VALUE method_raliasfile_init(VALUE self, char * alias_path)
{
rb_iv_set(self, "@alias_path", *alias_path);
return self;
}

declared in :

void Init_raliasfile() {
cRAliasFile = rb_define_class("RAliasFile", rb_cObject);
rb_define_singleton_method(cRAliasFile, "new",
method_raliasfile_new, 1);
rb_define_method(RAliasFile, "initialize",
method_raliasfile_init, 1);
rb_define_method(RAliasFile, "alias_path", method_alias_path,
0);
rb_define_method(RAliasFile, "orig_path", method_orig_path, 0);
rb_define_method(RAliasFile, "is_alias_file",
method_is_alias_file, 0);
rb_define_method(RAliasFile, "is_alias_broken",
method_is_alias_broken, 0);
rb_define_method(RAliasFile, "is_folder", method_is_folder, 0);
rb_define_method(RAliasFile, "is_data_file",
method_is_data_file, 0);
}


and i want the arg "alias_path" to be "global" to all of the methods
of this class, what to do for that ???

best,

Yvon

Eric Hodel

8/8/2006 11:24:00 PM

0

On Aug 8, 2006, at 3:55 PM, Yvon Thoraval wrote:

> Le 9 août 06 à 00:21, Eric Hodel a écrit :
>
>> On Aug 8, 2006, at 12:13 PM, Eric Hodel wrote:
>>
> [snip]
>> Oops.
>>
>> rb_define_method(rb_cMyClass, "initialize", cd_initialize, 1);
>
>
> ok thanks, i get it, now i've an init like that :
>
> VALUE method_raliasfile_init(VALUE self, char * alias_path)
> {
> rb_iv_set(self, "@alias_path", *alias_path);
> return self;
> }
>
> declared in :
>
> void Init_raliasfile() {
> cRAliasFile = rb_define_class("RAliasFile", rb_cObject);
> rb_define_singleton_method(cRAliasFile, "new",
> method_raliasfile_new, 1);

^^^ remove this line ^^^

You don't need to define new.

> rb_define_method(RAliasFile, "initialize",
> method_raliasfile_init, 1);
> rb_define_method(RAliasFile, "alias_path",
> method_alias_path, 0);
> rb_define_method(RAliasFile, "orig_path", method_orig_path,
> 0);
> rb_define_method(RAliasFile, "is_alias_file",
> method_is_alias_file, 0);
> rb_define_method(RAliasFile, "is_alias_broken",
> method_is_alias_broken, 0);
> rb_define_method(RAliasFile, "is_folder", method_is_folder,
> 0);
> rb_define_method(RAliasFile, "is_data_file",
> method_is_data_file, 0);
> }

--
Eric Hodel - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...



pere.noel

8/9/2006 5:39:00 AM

0

Eric Hodel <drbrain@segment7.net> wrote:

> > void Init_raliasfile() {
> > cRAliasFile = rb_define_class("RAliasFile", rb_cObject);
> > rb_define_singleton_method(cRAliasFile, "new",
> > method_raliasfile_new, 1);
>
> ^^^ remove this line ^^^
>
> You don't need to define new.

i get an eror now :
RAliasFile.c: In function 'method_alias_path':
RAliasFile.c:46: error: request for member 'alias_path' in something not
a structure or union

then, i think i have to do something like that :
typedef struct _raf {
int is_alias_file;
int is_folder;
int is_data_file;
char * alias_path;
char * orig_path;
char * version;
} RAliasFile;

--
une bévue

Nobuyoshi Nakada

8/9/2006 7:46:00 AM

0

Hi,

At Wed, 9 Aug 2006 07:55:52 +0900,
Yvon Thoraval wrote in [ruby-talk:207182]:
> VALUE method_raliasfile_init(VALUE self, char * alias_path)
> {
> rb_iv_set(self, "@alias_path", *alias_path);
> return self;
> }

All arguments are passed as VALUE, not C type.

VALUE method_raliasfile_init(VALUE self, VALUE alias_path)
{
rb_iv_set(self, "@alias_path", alias_path);
return self;
}

--
Nobu Nakada

pere.noel

8/9/2006 9:36:00 AM

0

<nobu@ruby-lang.org> wrote:

>
> All arguments are passed as VALUE, not C type.
>
> VALUE method_raliasfile_init(VALUE self, VALUE alias_path)
> {
> rb_iv_set(self, "@alias_path", alias_path);
> return self;
> }

then this arg "VALUE alias_path" will be common to all of the methods i
have ?

it seems not (???)

i've change my init for debugg purpose to :


VALUE m_raliasfile_init(VALUE self, VALUE alias_path)
{
printf("m_raliasfile_init");
printf((char *)alias_path);
printf("alias_path");
rb_iv_set(self, "@alias_path", alias_path);
return self;
}


and, when i use my class from ruby i do :

a=RAliasFile.new("/Users/yvon/work/Ruby/Native/C/doc/Introduction_ANSI_C
_html")
puts a.alias_path

the print out (from C) being :

m_raliasfile_initalias_path

which means the arg "alias_path" isn't pass to "m_raliasfile_init"

the ruby line "puts a.alias_path" gives :

ArgumentError: NULL pointer given

where the C counterpart being :
VALUE m_alias_path(VALUE self) {
// char *alias_path = "/path/to/something/undefined";
return rb_str_new2((char *) alias_path);
}


what i want is my class "RAliasFile" being initialize like that (from
ruby) :

a=RAliasFile.new("/Users/yvon/work/Ruby/Native/C/doc/Introduction_ANSI_C
_html")

the parameter of "new" being alias_path bar in the C counterpart...

i'm a newb in C as you can see, that's (will be) my first extension..
--
une bévue

ts

8/9/2006 9:50:00 AM

0

>>>>> "U" == =?ISO-8859-1?Q?Une b=E9vue?= <pere.noel@laponie.com.invalid> writes:


Post your *complete* source

U> where the C counterpart being :
U> VALUE m_alias_path(VALUE self) {
U> // char *alias_path = "/path/to/something/undefined";
U> return rb_str_new2((char *) alias_path);
U> }

This is just a nonsense. You define an instance variable @alias_path then
you try to retrieve a local C variable alias_path rather than the instance
variable.


Guy Decoux