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


[code]
#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);
}
[/code]

and my ruby test file is:

[code]
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"
[/code]

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

Thanks in advance, Duane.

-- 
Posted via http://www.ruby-forum.com/.