Question about Ruby extension API

I’ve looked all over but can’t find the function call for doing a simple assignment (!)

Within a method (set via rb_define_method and later called by the user), I would like to do the equivalent of:
obj = otherobj.method
I tried calling rb_intern on “=”, and using rb_funcall( obj, (IdFromRBIntern), 1, (value from call of other method) ) but I get an ‘undefined method’ error from ruby at runtime.

Thanks,
Patrick Bennett

Hi,

I’ve looked all over but can’t find the function call for
doing a simple assignment (!)

rb_gv_set() for global variables,
rb_iv_set() for instance variables,
rb_cv_set() for class variables.

Within a method (set via rb_define_method and later called by
the user), I would like to do the equivalent of:
obj = otherobj.method
I tried calling rb_intern on “=”, and using rb_funcall( obj,
(IdFromRBIntern), 1, (value from call of other method) ) but
I get an ‘undefined method’ error from ruby at runtime.

Assignment is not a method.

···

At Tue, 27 Aug 2002 06:45:04 +0900, Bennett, Patrick Patrick.Bennett@inin.com wrote:


Nobu Nakada

Hi,

I’ve looked all over but can’t find the function call for doing a simple assignment (!)

It depends on what you want to assign to.

If it is a local variable, reconsider your design. You cannot modify
local variables from within a C function, since a C function is a
method, and a method cannot modify local variables of outer methods.

If it is an instance variable, use rb_ivar_set(). For a global
variable, rb_gvar_set() is available.

						matz.
···

In message “Question about Ruby extension API” on 02/08/27, “Bennett, Patrick” Patrick.Bennett@inin.com writes:

Hi,

I’ve looked all over but can’t find the function call for
doing a simple assignment (!)

rb_gv_set() for global variables,
rb_iv_set() for instance variables,
rb_cv_set() for class variables.

I forgot rb_lastline_set() and rb_backref_set() for $_ and $~,
respectively.

Within a method (set via rb_define_method and later called by
the user), I would like to do the equivalent of:
obj = otherobj.method
I tried calling rb_intern on “=”, and using rb_funcall( obj,
(IdFromRBIntern), 1, (value from call of other method) ) but
I get an ‘undefined method’ error from ruby at runtime.

Assignment is not a method.

Of cource, you can use attr_writer, such as “foo=”.

rb_funcall(obj, rb_intern(“foo=”), 1, value);

···

At Tue, 27 Aug 2002 11:19:21 +0900, nobu.nokada@softhome.net wrote:

At Tue, 27 Aug 2002 06:45:04 +0900, > Bennett, Patrick Patrick.Bennett@inin.com wrote:


Nobu Nakada

In my specific case, I have a Time-derived class that I created through
the API, and I have a method that needs to adjust the underlying time by
a specific offset.
I’m trying to do the equivalent of self.+= (fixnum) but it doesn’t work.
Why wouldn’t I be able to call += or = for my derived-class ?

The class is very simple and adds an instance variable (with
getter/setter), and a method for ‘fixing up’ the time (for a specific
need). Unfortunately, I haven’t found a way for the ‘fix up’ method to
modify itself via the inherited Time methods.
It’s a pretty simple requirement, and it’s kind of frustrating to run
into this brick wall after having handled the rest of the API ‘fairly
well’. It seems odd that a derived-class of Time can’t even modify itself.

If I’m reading correctly though, are you saying that I’ll have to make
my method create a new time object, add to it (I’m assuming I >can< call
Time#+() ?) and return the the new instance ? (forcing the user to call
x = x.fixup)

Thanks,
Patrick Bennett

Yukihiro Matsumoto wrote:

···

Hi,

In message “Question about Ruby extension API” > on 02/08/27, “Bennett, Patrick” Patrick.Bennett@inin.com writes:

I’ve looked all over but can’t find the function call for doing a simple assignment (!)

It depends on what you want to assign to.

If it is a local variable, reconsider your design. You cannot modify
local variables from within a C function, since a C function is a
method, and a method cannot modify local variables of outer methods.

If it is an instance variable, use rb_ivar_set(). For a global
variable, rb_gvar_set() is available.

  					matz.

Hi,

···

In message “Re: Question about Ruby extension API” on 02/08/27, Patrick Bennett patrick.bennett@inin.com writes:

In my specific case, I have a Time-derived class that I created through
the API, and I have a method that needs to adjust the underlying time by
a specific offset.
I’m trying to do the equivalent of self.+= (fixnum) but it doesn’t work.
Why wouldn’t I be able to call += or = for my derived-class ?

“a+=b” is a syntax sugar for “a = a + b”, so that it cannot be
redefined (since it is not a method). you have to redefine the "+"
method.

						matz.