Hi,

>From: duane bailey <bailey.d.r / gmail.com>
>Reply-To: ruby-talk / ruby-lang.org
>To: ruby-talk / ruby-lang.org (ruby-talk ML)
>Subject: extenion problems :(
>Date: Tue, 27 Jun 2006 03:51:06 +0900
>
>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.
>
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 Heesob