I know it’s possible to write Ruby in C but is it possible to instantiate
a Ruby object (defined only in a Ruby class; no C) in a C extention and
then call methods on that object in C?
I know we can instantiate Strings (rb_str_new2(“some string”)), Arrays
(rb_ary_new), and Hashes for example in a C extention, but these classes
are all defined in C.
I’m thinking it’s probably not possible to do this, but maybe there’s a
trick to it that I don’t know about… Otherwise I’ll just have to rewrite
several Ruby classes in RubyC.
Sorry if that was too short as to be unhelpful. What I meant was that, if
you have the class object on the C side, you can just call `new’ on it with
one of these:
rb_funcall
rb_funcall2
rb_funcall3
rb_apply
You will need to get the id for `new’ first, with this:
Maybe I’m not understanding, but can’t you just call `new’ on the class you
want to instantiate?
Sorry if that was too short as to be unhelpful. What I meant was that, if
you have the class object on the C side, you can just call `new’ on it with
one of these:
rb_funcall
rb_funcall2
rb_funcall3
rb_apply
You will need to get the id for `new’ first, with this:
new_id = rb_intern (“new”);
Most of this is on page 193 of the pickaxe.
Or, you could just do this (which is slower):
rb_eval_string (“YourRubyClass.new”);
Hmmmm… but how does the C side ‘know’ where my Ruby class is defined?
There doesn’t seem to be a ‘rb_require’. I guess I should just try it
out. I suppose if on the Ruby side I’ve got:
require ‘SomeClass’
require ‘MyCExtention’
That perhaps MyCExtention might then have access to SomeClass (?)
Maybe I’m not understanding, but can’t you just call `new’ on the class you
want to instantiate?
Sorry if that was too short as to be unhelpful. What I meant was that, if
you have the class object on the C side, you can just call `new’ on it with
one of these:
rb_funcall
rb_funcall2
rb_funcall3
rb_apply
You will need to get the id for `new’ first, with this:
new_id = rb_intern (“new”);
Most of this is on page 193 of the pickaxe.
Or, you could just do this (which is slower):
rb_eval_string (“YourRubyClass.new”);
Hmmmm… but how does the C side ‘know’ where my Ruby class is defined?
There doesn’t seem to be a ‘rb_require’. I guess I should just try it
out. I suppose if on the Ruby side I’ve got:
require ‘SomeClass’
require ‘MyCExtention’
That perhaps MyCExtention might then have access to SomeClass (?)
OK, I tried it and that is how it seems to work.
It seems like to call the constructor of a Ruby class from C that you
have to use rb_eval_string(“YourRubyClass.new”); I don’t see how you
could do it otherwise since rb_funcall needs a receiver and you don’t yet
have a reciever:
rb_funcall(VALUE recv, ID id, int argc, …);
So how would you call Foo.new using rb_funcall? You’d have to get the ID:
rb_intern(“Foo.new”);
But then what would you use for recv?
At any rate, rb_eval_string is working fine for me.
It seems like to call the constructor of a Ruby class from C that you
have to use rb_eval_string(“YourRubyClass.new”); I don’t see how you
could do it otherwise since rb_funcall needs a receiver and you don’t yet
have a reciever:
rb_funcall(VALUE recv, ID id, int argc, …);
So how would you call Foo.new using rb_funcall? You’d have to get the ID:
rb_intern(“Foo.new”);
But then what would you use for recv?
You can get the value of constant “Foo” (seems like rb_gv_get() will do the
job, I may be mistaken) and use it as a receiver.
At any rate, rb_eval_string is working fine for me.
Phil
Gennady.
···
----- Original Message -----
From: “Phil Tomson” ptkwt@shell1.aracnet.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, April 24, 2003 2:33 PM
Subject: Re: Accessing Ruby class from C extention
It seems like to call the constructor of a Ruby class from C that you
have to use rb_eval_string(“YourRubyClass.new”); I don’t see how you
could do it otherwise since rb_funcall needs a receiver and you don’t yet
have a reciever:
rb_funcall(VALUE recv, ID id, int argc, …);
So how would you call Foo.new using rb_funcall? You’d have to get the ID:
rb_intern(“Foo.new”);
But then what would you use for recv?
From: “Phil Tomson” ptkwt@shell1.aracnet.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, April 24, 2003 2:33 PM
Subject: Re: Accessing Ruby class from C extention
It seems like to call the constructor of a Ruby class from C that you
have to use rb_eval_string(“YourRubyClass.new”); I don’t see how you
could do it otherwise since rb_funcall needs a receiver and you don’t
yet
have a reciever:
rb_funcall(VALUE recv, ID id, int argc, …);
So how would you call Foo.new using rb_funcall? You’d have to get the
ID:
rb_intern(“Foo.new”);
But then what would you use for recv?
You can get the value of constant “Foo” (seems like rb_gv_get() will do
the
job, I may be mistaken) and use it as a receiver.
I mean, as a receiver for “new”.
···
----- Original Message -----
From: “Gennady” gfb@tonesoft.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, April 24, 2003 3:34 PM
Subject: Re: Accessing Ruby class from C extention
----- Original Message -----
At any rate, rb_eval_string is working fine for me.
Kool! I wasn’t aware of this…
I will right away add this to my embedding-tutorial
Is your tutorial online? I did a quick google search for it, but could find
nothing. If it is online I would really like to read it (in that case is
there a URL?).
Take care, Matthew
···
–
A friend is someone who knows everything about you and likes you in
spite of it.
-Mark Twain
Kool! I wasn’t aware of this…
I will right away add this to my embedding-tutorial
Is your tutorial online? I did a quick google search for it, but could find
nothing. If it is online I would really like to read it (in that case is
there a URL?).