Forum: Ruby c api, getting class

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ef5256760771d0c5cad01dc3b35382ba?d=identicon&s=25 Patrick Gundlach (Guest)
on 2006-02-08 12:43
(Received via mailing list)
Hi,

q: How do I throw a custom exception in the c-part of a ruby program?


I have a mixed ruby/c class and I'd like to throw an exception that is
defined in the ruby side from the c side:


myklass.rb

class Myclass
  class UnknownFormat < Exception
  end
end


myklass_c.c

....
  VALUE eUnknownFormatError;
  eUnknownFormatError=rb_gv_get("Myclass::UnknownFormat");
  rb_raise(eUnknownFormatError,"fatal: format not found");


This does not work. I have no idea how to proceed. Some small hints
would probably enough to point me in the right direction. I have seen
(pickaxe) that I can define the class in the c part with
rb_define_class(), but I want to reuse the class that is already
defined on the ruby side.

Patrick
D84df7c68f790e492c4ad4ec5fe65547?d=identicon&s=25 Florian Frank (Guest)
on 2006-02-08 13:17
(Received via mailing list)
On 2006-02-08 20:43:21 +0900, Patrick Gundlach wrote:
> This does not work. I have no idea how to proceed. Some small hints
> would probably enough to point me in the right direction. I have seen
> (pickaxe) that I can define the class in the c part with
> rb_define_class(), but I want to reuse the class that is already
> defined on the ruby side.

Use rb_path2class("Myclass::UnknownFormat").
Ef5256760771d0c5cad01dc3b35382ba?d=identicon&s=25 Patrick Gundlach (Guest)
on 2006-02-08 13:35
(Received via mailing list)
Hello Florian,

> On 2006-02-08 20:43:21 +0900, Patrick Gundlach wrote:
>> This does not work. I have no idea how to proceed. Some small hints
>> would probably enough to point me in the right direction. I have seen
>> (pickaxe) that I can define the class in the c part with
>> rb_define_class(), but I want to reuse the class that is already
>> defined on the ruby side.
>
> Use rb_path2class("Myclass::UnknownFormat").

This is exactly what I was looking for; seems somewhat undocumented.

Thank you,

  Patrick
This topic is locked and can not be replied to.