[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Trouble with rb_define_method("var="

zarawesome

10/11/2004 2:42:00 AM

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.
1 Answer

Jamis Buck

10/11/2004 2:57:00 AM

0

Guilherme T. wrote:
> ----- 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

--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck...