Method Call from inside a file

I coded a new method within gc.c file defining as–

static VALUE rb_obj_ manualfree(){
printf(“Hello, I am Manualfree.Nice to meet you”);
}

And calling this method from my *.rb file as-

GC.manualfree();

But when I am running the code its giving me this error–

dougtest.rb:4: undefined method `manualfree’ for GC:Module
(NoMethodError)

What do i need to do… so that I can call the new function and print out
that message.

Regards
Tridib

Tridib B. wrote in post #988313:

What do i need to do… so that I can call the new function and print out
that message.

rb_define_method().

For more info see
http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html
and/or pretty much any .c file in the ruby source.

Brian C. wrote in post #988349:

rb_define_method().

Where should add this line within that function or create a new one
under Init.

Regards

Tridib

Okay i searched everywhere in Google but they are suggesting me to
create a new “extconf.rb” file which looks very confusing to me.

Now my code looks like this under gc.c file.>

void Init_Test();
static VALUE rb_obj_ manualfree();
VALUE manual = Qnil;

static VALUE rb_obj_ manualfree(){
printf(“Hello, I am Manualfree.Nice to meet you”);
}

void Init_Test(){
rb_define_method(manual,“test”,manualfree,0);
}

And I am calling the manualfree function like>

GC.manual();

But its giving me the same error…

dougtest.rb:4: undefined method `manualfree’ for GC:Module
(NoMethodError)

What else should I have to do?

Tridib

Tridib B. [email protected] wrote:

Brian C. wrote in post #988349:

rb_define_method().

Where should add this line within that function or create a new one
under Init.

Really, 30 seconds with Google would have answered this question 20
times
faster than posting to a newsgroup.

I gave you a link to the page with the answer already:

http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html

Click on that link. Read it all. Look where they call
rb_define_method(), and do the same yourself.

Hello

Thanks a lot it is working and giving me the desired output.

But I am little confused about two things.

  1. Is the Init_Name I am declaring and calling from the ruby file, Is it
    acting like a Library function? If not, how to create a library
    function?

  2. What does the require command do?

Regards

Tridib

Brian C. wrote in post #990438:

Your code will be either compiled into a shared library (.so or .dll),

Yes its getting compiled in *.so file.

I don’t know what you mean by “acting like a library function”.

I mean to say that I am trying to write a function which will allow the
users to free the allocated memory by themselves rather than doing it by
Garbage Collection. So I need a syntax like that of C

free(variable);

So, to achieve it do I need to this above detailed thing or I have to do
any other stuffs.

Regards

Tridib

Tridib B. wrote in post #990410:

  1. Is the Init_Name I am declaring and calling from the ruby file, Is it
    acting like a Library function? If not, how to create a library
    function?

“Now look at the last function, Init_Test. Every class or module defines
a C global function named Init_ Name. This function will be called when
the interpreter first loads the extension Name (or on startup for
statically linked extensions). It is used to initialize the extension
and to insinuate it into the Ruby environment.”

I don’t know what you mean by “acting like a library function”. Your
code will be either compiled into a shared library (.so or .dll), or
linked directly into the ruby binary, depending on how you build it.
Init_ is a publicly-accessible function, which is called by ruby
when the module is loaded, or when the interpreter starts.

  1. What does the require command do?

http://www.ruby-doc.org/core/classes/Kernel.html#M001418

Tridib B. wrote in post #990442:

I don’t know what you mean by “acting like a library function”.

I mean to say that I am trying to write a function which will allow the
users to free the allocated memory by themselves rather than doing it by
Garbage Collection. So I need a syntax like that of C

free(variable);

I believe you’re writing C code for use by ruby. Ruby doesn’t provide
users with any free() method; users rely on garbage collection. So if
you want to extend ruby in C, you need to make your code work with
ruby’s garbage collection.

A ruby user can prevent something from being garbage collected, if
they want, just by holding a reference to it (e.g. in a global
variable).

If you read
http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html then
you’ll find whole sections on memory allocation and garbage collection.
Look at Data_Make_Struct and Data_Wrap_Struct in particular. If you
won’t read the documentation, then I’m afraid you’re on your own.

Of course, if you’re writing C to be called from C (rather than from
ruby) then you can have people call your own allocate and free routines.
But this is not a forum about writing C programs.

Ruby doesn’t provide users with any free() method; users rely on garbage
collection.

I want to switch off the Garbage Collection and do my own memory
deallocation.So how can I achieve that? I know I have to write a own
code of mine, but I don’t understand how to execute my own code from the
Ruby gc.c file.

Regards

Tridib

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs