Ask a Question related to Ruby, Design and Development.
-
Bennett, Patrick #1
Problem with exception class and instance vars
I'm having problems with my Ruby module.
The instance variables I'm setting in a custom exception class don't
appear to be getting set.
What am I doing wrong?
Declaration code:
EventExceptionKlass = rb_define_class("EventException",
rb_eStandardError);
rb_define_method(EventExceptionKlass, "event",
VALUEFUNC(EventExcepGetEvent), 0); // just calls return
rb_iv_get(self,"@event")
rb_define_method(EventExceptionKlass, "msg",
VALUEFUNC(EventExcepGetMessage), 0); // just calls return
rb_iv_get(self,"@msg")
Code where exception is raised.
catch (const RecoCommonLib::EventException& ex)
{
VALUE throwex = rb_exc_new2( EventExceptionKlass, ex.what());
rb_iv_set(throwex, "@event", rb_str_new2( [pointer to some
string]) );
rb_iv_set(throwex, "@msg", rb_str_new2( [pointer to another
string])) );
rb_exc_raise(throwex);
}
When I catch the Ruby "EventException" class in a rescue class (which
happens correctly) then 'event' and 'msg' are always nil (if I test
them).
...
rescue EventException => ex
puts "Event:#{ex.event} Msg:#{ex.msg} Exception:#{ex}"
end
Bennett, Patrick Guest
-
RFC: instance::vars
I've put together something that I don't know what to call. My working name is "instance::vars", in lowercase because it's similar to the "use... -
#26364 [Opn->Bgs]: initializing class in other class vars value problem
ID: 26364 Updated by: sniper@php.net Reported By: brightone at o2 dot pl -Status: Open +Status: ... -
#26364 [Bgs->Opn]: initializing class in other class vars value problem
ID: 26364 User updated by: brightone at o2 dot pl Reported By: brightone at o2 dot pl -Status: Bogus +Status: ... -
#26364 [NEW]: initializing class in other class vars value problem
From: brightone at o2 dot pl Operating system: windows xp PHP version: 4.3.4 PHP Bug Type: Class/Object related Bug... -
Instance- and class-variables (was mixing in class methods)
Hi -- On Thu, 2 Oct 2003, Mark J. Reed wrote: Also, every object should have the right to maintain state in instance variables that are... -
ts #2
Re: Problem with exception class and instance vars
>>>>> "B" == Bennett, Patrick <Patrick.Bennett@inin.com> writes:
B> What am I doing wrong?
Difficult to say : probably ruby don't like C++ :-))
svg% cat aa.c
#include <ruby.h>
static VALUE evx;
static VALUE
aa_tt(obj)
VALUE obj;
{
VALUE thr = rb_exc_new2(evx, "<exception>");
rb_iv_set(thr, "@event", rb_str_new2("<string>"));
rb_iv_set(thr, "@msg", rb_str_new2("<another string>"));
rb_exc_raise(thr);
}
static VALUE
aa_event(obj)
VALUE obj;
{
return rb_iv_get(obj, "@event");
}
static VALUE
aa_msg(obj)
VALUE obj;
{
return rb_iv_get(obj, "@msg");
}
void Init_aa()
{
evx = rb_define_class("EventException", rb_eStandardError);
rb_define_method(evx, "event", aa_event, 0);
rb_define_method(evx, "msg", aa_msg, 0);
rb_define_global_function("tt", aa_tt, 0);
}
svg%
svg% cat b.rb
#!/usr/bin/ruby
require 'aa'
begin
tt
rescue EventException => ex
puts "Event : #{ex.event} Msg : #{ex.msg} Exception : #{ex}"
end
svg%
svg% b.rb
Event : <string> Msg : <another string> Exception : <exception>
svg%
Guy Decoux
ts Guest
-
Bennett, Patrick #3
Re: Problem with exception class and instance vars
Ah-ha! (or, Doh!) I had defined my 'get' methods with the wrong
signature.
I had set them up as if they were rb_define_method(..., -1) methods
taking (argc, argv, self) instead of (self)
Your test example made me realize the error of my ways. ;)
Thanks!!
Patrick Bennett
-----Original Message-----
From: ts [mailto:decoux@moulon.inra.fr]
Sent: Thursday, November 13, 2003 11:41 AM
To: ruby-talk ML
Cc: [email]ruby-talk@ruby-lang.org[/email]
Subject: Re: Problem with exception class and instance vars
B> What am I doing wrong?>>>>> "B" == Bennett, Patrick <Patrick.Bennett@inin.com> writes:
Difficult to say : probably ruby don't like C++ :-))
svg% cat aa.c
#include <ruby.h>
static VALUE evx;
static VALUE
aa_tt(obj)
VALUE obj;
{
VALUE thr = rb_exc_new2(evx, "<exception>");
rb_iv_set(thr, "@event", rb_str_new2("<string>"));
rb_iv_set(thr, "@msg", rb_str_new2("<another string>"));
rb_exc_raise(thr);
}
static VALUE
aa_event(obj)
VALUE obj;
{
return rb_iv_get(obj, "@event");
}
static VALUE
aa_msg(obj)
VALUE obj;
{
return rb_iv_get(obj, "@msg");
}
void Init_aa()
{
evx = rb_define_class("EventException", rb_eStandardError);
rb_define_method(evx, "event", aa_event, 0);
rb_define_method(evx, "msg", aa_msg, 0);
rb_define_global_function("tt", aa_tt, 0);
}
svg%
svg% cat b.rb
#!/usr/bin/ruby
require 'aa'
begin
tt
rescue EventException => ex
puts "Event : #{ex.event} Msg : #{ex.msg} Exception : #{ex}" end
svg%
svg% b.rb
Event : <string> Msg : <another string> Exception : <exception> svg%
Guy Decoux
Bennett, Patrick Guest



Reply With Quote

