# Extenion problems :(

**URL:** <https://www.ruby-forum.com/t/extenion-problems/96151>\
**Category:** Ruby\
**Created:** [June 26, 2006, 8:50pm UTC](https://www.ruby-forum.com/t/extenion-problems/96151 "2006-06-26T20:50:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nayr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3d9bf3/32.png) [@nayr](https://www.ruby-forum.com/u/nayr)\
**Post date:** [June 26, 2006, 8:50pm UTC](https://www.ruby-forum.com/t/extenion-problems/96151/1 "2006-06-26T20:50:53Z")

</div>

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

```auto
#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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![nayr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3d9bf3/32.png) [@nayr](https://www.ruby-forum.com/u/nayr)\
**Post date:** [June 27, 2006, 2:36am UTC](https://www.ruby-forum.com/t/extenion-problems/96151/2 "2006-06-27T02:36:31Z")

</div>

Hi,

> ```auto
> 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.

---

<div class="post-metadata">

**Author:** ![nayr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3d9bf3/32.png) [@nayr](https://www.ruby-forum.com/u/nayr)\
**Post date:** [June 27, 2006, 4:43am UTC](https://www.ruby-forum.com/t/extenion-problems/96151/3 "2006-06-27T04:43:54Z")

</div>

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);
