From: Charles Mills Date: 2005-05-19T07:30:13+09:00 Subject: Re: howto make something like Errno::##, for my C ext, or do I need to? Sam Roberts wrote: > Thanks for your help, Charlie. > > Charlie Mills wrote: > > In short: > > rb_exc_raise(my_err_new(eno)); > > Took me a minute to find that... it's in intern.h, can I call those > functions, I mean, should I? > It is OK to call those... I asked about this once: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/90163 http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/1807 > Looking through that code, it seems my only option is to create the > object myself, then call this internal function. > > Looks like it will work fine, but is this really recommended? It is fine. You also need to create your error class: class MyErr < StandardError; end # or in C: eMyError = rb_define_class("MyError", rb_eStandardError); Then you can define an accessor in Ruby or in C: attr_reader :eno rb_define_attr(eMyError, "eno", 1, 0); I wouldn't worry about writing an init function if your only going to be raising these errors from C. You can instantiate/raise the errors as I described in the earlier post. -Charlie > > Daniel wrote: > > Doesn't rb_sys_fail(0) work for this scenario? I thought if a C > > function failed, you could call rb_sys_fail(0) and it would raise the > > appropriate Errno. Or am I confused? > > I didn't know about this function, but no, its not for me. > > It only makes sense if the C function you are calling is returning a > system error number, i.e. from . > > For example, when a C function in our library returns 9, it means "index > out of bounds", but when a system call returns 9 it means bad file > descriptor, and when the Crypoki API (random example) returns 9 it means > CKR_NEED_TO_CREATE_THREADS. > > I wouldn't want to return Errno::EBADF from my wrapper when the error is > index out of bounds! > > Charlie wrote: > > Yeah that works too, if your using the standard library functions. I > > thought from the OP that the error numbers he was working with were not > > related to those used by the std lib, but I may be wrong. > > Nope, bang on. > > Sam > > -- > http://www.certicom.com