Method Call from inside a file.

  • Thread starter Tridib Bandopadhyay
  • Start date
T

Tridib Bandopadhyay

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
 
T

Tridib Bandopadhyay

Brian Candler wrote in post #988349:
rb_define_method().

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

Regards

Tridib
 
T

Tridib Bandopadhyay

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
 
T

Tridib Bandopadhyay

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
 
B

Brian Candler

Tridib Bandopadhyay 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.
2. What does the require command do?

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

Tridib Bandopadhyay

Brian Candler 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
 
B

Brian Candler

Tridib Bandopadhyay wrote in post #990442:
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.
 
T

Tridib Bandopadhyay

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,217
Latest member
topweb3twitterchannels

Latest Threads

Top