C function rb_raise doesn't behave the same as Ruby keyword 'raise'

A

Asfand Yar Qazi

Hi,

Where in Ruby, you can do a:

e = StandardError.new("Bog")
raise(e)

in C, you can't do the equivalent:

VALUE exobj = rb_funcall(eMyEx, rb_intern("new"), 0);
rb_raise(exobj, "Bog");

Why?

If I wanted to do this, how could I?

--
Entry in RollerCoaster Tycoon 2 readme.txt file:
RollerCoaster Tycoon2 must be played on a video card capable of 640x480 screen
resolution at a bit depth setting of 256 bits.
And the proof that playing too many strategy games causes loss of humour:
http://tinyurl.com/dyrtt
 
R

rmagick

rb_funcall lets you pass arguments to the method. In this case, pass
the Ruby String version of "Bog".

VALUE bog = rb_str_new2("Bog");
VALUE exobj = rb_funcall(eMyEx, rb_intern("new"), 1, bog);

rb_raise(exobj, "");
 
A

Asfand Yar Qazi

rb_funcall lets you pass arguments to the method. In this case, pass
the Ruby String version of "Bog".

VALUE bog = rb_str_new2("Bog");
VALUE exobj = rb_funcall(eMyEx, rb_intern("new"), 1, bog);

rb_raise(exobj, "");

Argh, perhaps I didn't explain this too well.

You don't understand - the line 'rb_raise(exobj, "");' would not do what was
expected. It would (under ruby 1.8.2 last time I checked) crash the interpreter
or (under 1.8.4) issue a NoMethodError exception saying 'undefined method `new'
for #<Mod::MyEx: Mod::MyEx>' or something similar. I.e. rb_raise tries to call
'new' on its first argument, and if that's a non-Class object, then....

Which gives me an idea on how to kludge together something that works..... a
'new' instance method that returns an identical copy of the object it is called
on.... hmm....... *loses himself in deep thought*
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top