Extenion problems :(

I started to write a vector extension to ruby, as a way to get familiar
with it.

#include "ruby.h"
#include "Vector.h"
#include <stdlib.h>

static void V3Free(void *p)
{
}

static VALUE V3Alloc(VALUE klass)
{
	Vector3 v = Vector3Zero;
	VALUE obj;
	obj = Data_Wrap_Struct(klass, 0, V3Free, &v);
}

static VALUE getX(VALUE self)
{
	Vector3 *v;

	Data_Get_Struct(self, Vector3, v);

	return rb_float_new(v->x);
}

static VALUE getY(VALUE self)
{
	Vector3 *v;

	Data_Get_Struct(self, Vector3, v);

	return rb_float_new(v->y);
}

static VALUE getZ(VALUE self)
{
	Vector3 *v;

	Data_Get_Struct(self, Vector3, v);

	return rb_float_new(v->z);
}

static VALUE rbVector3Initialize(VALUE self, VALUE x, VALUE y, VALUE z)
{
	Vector3 *v;

	Data_Get_Struct(self, Vector3, v);

	v->x = (float)NUM2DBL(x);
	v->y = (float)NUM2DBL(y);
	v->z = (float)NUM2DBL(z);

	return self;
}

VALUE rbVector3Object;

void Init_Vector()
{
	rbVector3Object = rb_define_class("Vector3", rb_cObject);
	rb_define_alloc_func(rbVector3Object, V3Alloc);
	rb_define_method(rbVector3Object, "initialize", rbVector3Initialize, 
3);
	rb_define_method(rbVector3Object, "x", getX, 0);
	rb_define_method(rbVector3Object, "y", getY, 0);
	rb_define_method(rbVector3Object, "z", getZ, 0);
}

and my ruby test file is:

require 'Vector'

v = Vector3.new(1, 2, 3)

puts "X: " + v.x.to_s + "\n"
puts "Y: " + v.y.to_s + "\n"
puts "Z: " + v.z.to_s + "\n"

The problem is, it outputs Z to be 0, instead of 3. what’s wrong? i’m
completely baffled.

Thanks in advance, Duane.

Hi,

	Vector3 v = Vector3Zero;
>	return rb_float_new(v->x);
>
>{
>
>	rb_define_method(rbVector3Object, "z", getZ, 0);
>puts "X: " + v.x.to_s + "\n"
>puts "Y: " + v.y.to_s + "\n"
>puts "Z: " + v.z.to_s + "\n"

The problem is, it outputs Z to be 0, instead of 3. what’s wrong? i’m
completely baffled.

Thanks in advance, Duane.

static VALUE V3Alloc(VALUE klass)
{
Vector3 v = Vector3Zero;
VALUE obj;
obj = Data_Wrap_Struct(klass, 0, V3Free, &v);
}

should be

static VALUE V3Alloc(VALUE klass)
{
static Vector3 v;
VALUE obj;
obj = Data_Wrap_Struct(klass, 0, V3Free, &v);
return obj;
}

Regards,

Park H.

Hi,

At Tue, 27 Jun 2006 03:51:06 +0900,
duane bailey wrote in [ruby-talk:199150]:

static VALUE V3Alloc(VALUE klass)
{
Vector3 *v = ALLOC(Vector3);
*v = Vector3Zero;
rturn Data_Wrap_Struct(klass, 0, -1, v);