Trouble with rb_define_method("var=")

G

Guilherme T.

I'm having trouble getting Ruby to acknowledge writing methods (of the
type "var="). It seems to ignore all such commands.

These are the relevant parts of the code;

----- C code

static VALUE t_get_xs(VALUE self)
{
s_entity *e;
Log("Getting xs");
Data_Get_Struct(self, s_entity, e);
double f=e->xs;
return rb_float_new(((double)e->xs));
}

static VALUE t_set_xs(VALUE self, VALUE arg1)
{
s_entity *e;
Log("Setting xs");
Data_Get_Struct(self, s_entity, e);
e->xs=NUM2DBL(arg1);
return arg1;
}

void ScriptPrepareClasses()
{
VALUE class_entity=rb_define_class("Entity",rb_cObject);

rb_define_method(class_entity, "xs", RUBY_METHOD_FUNC(t_get_xs), 0);
rb_define_method(class_entity, "xs=", RUBY_METHOD_FUNC(t_set_xs),
1);
}

----- Ruby code

class EntityB < Entity
def daemon()
@a=xs
xs=0.1
xs=xs
end
end

-----

When creating an object of class 'EntityB' via Data_Wrap_Struct and
launching its daemon() method, only the first line (@a=xs) will
execute properly. The others are ignored, and t_set_xs is never
called.

Any opinions as to why this happens are appreciated. Thanks for the
attention.
 
J

Jamis Buck

Guilherme said:
----- Ruby code

class EntityB < Entity
def daemon()
@a=xs
xs=0.1
xs=xs
end
end

Assignments to setter methods like this require an explicit reciever, so
you have to say:

self.xs = 0.1

Otherwise, Ruby interprets the 'xs' as a local variable.

- Jamis
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top