# Wrapping a structure as a new class

**URL:** <https://www.ruby-forum.com/t/wrapping-a-structure-as-a-new-class/106285>\
**Category:** Ruby\
**Created:** [June 8, 2007, 3:47am UTC](https://www.ruby-forum.com/t/wrapping-a-structure-as-a-new-class/106285 "2007-06-08T03:47:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![trebor777](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a9adbd/32.png) [@trebor777](https://www.ruby-forum.com/u/trebor777)\
**Post date:** [June 8, 2007, 3:47am UTC](https://www.ruby-forum.com/t/wrapping-a-structure-as-a-new-class/106285/1 "2007-06-08T03:47:11Z")

</div>

well i don’t know if my title is exactly what i mean…  
anyway

I’m writing a C extension, which can create a Bitmap class.(I’m using  
SDL)

So far, it’s working… for the initialization:

//========================================  
VALUE cBitmap;

void sdl\_freeSurface(SDL\_Surface\* surface)  
{  
SDL\_FreeSurface(surface);  
}

VALUE bitmap\_init(int argc, VALUE \*argv, VALUE self)  
{  
SDL\_Surface \*new\_bitmap;

if (argc == 1)  
{  
Check\_Type(argv[0],T\_STRING);  
new\_bitmap=IMG\_Load(STR2CSTR(argv[0]));  
}  
else  
{  
Check\_Type(argv[0],T\_FIXNUM);  
Check\_Type(argv[1],T\_FIXNUM);

new\_bitmap=SDL\_CreateRGBSurface(SDL\_HWSURFACE,FIX2UINT(argv[0]),FIX2UINT(argv[1]),32,0,0,0,0);  
}  
return Data\_Wrap\_Struct(cBitmap,0,sdl\_freeSurface,new\_bitmap);  
}  
//=========================================================

when i do a=Bitmap.new(23,32)  
i got a bitmap object.  
but here comes the problem:

I got an argument type error on the class methods…(a.width for  
example):  
"wrong argument type Bitmap "

I’ve understand that the wrapped structure is a T\_DATA object… but  
all I  
want is to access this structure.  
a class method example using which try to access the struct:  
//===============================  
VALUE bitmap\_width(VALUE obj)  
{  
SDL\_Surface \*surface;  
Data\_Get\_Struct(obj,SDL\_Surface,surface);  
return INT2FIX(surface-\>w);  
}  
//===============================

If i use the ‘self’ argument in the Data\_wrap instead of cBitmap, I  
can’t  
create the object and got the same error: '… type Bitmap  
’

So if someone can explain me 😛 how to get back my structure…please.  
^^  
would be great!

---

<div class="post-metadata">

**Author:** ![trebor777](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a9adbd/32.png) [@trebor777](https://www.ruby-forum.com/u/trebor777)\
**Post date:** [June 10, 2007, 9:30pm UTC](https://www.ruby-forum.com/t/wrapping-a-structure-as-a-new-class/106285/2 "2007-06-10T21:30:02Z")

</div>

Arf^^… nobody answered, but it’s ok, I’ve solved my problem…  
It’s just because, i didnt’ defined the initialize method as a singleton  
method (other tutorials never told me to…, and it has always worked  
so…)  
Anyway, it works now! Raaaah it was so simple xD

---

<div class="post-metadata">

**Author:** ![trebor777](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a9adbd/32.png) [@trebor777](https://www.ruby-forum.com/u/trebor777)\
**Post date:** [June 11, 2007, 5:11am UTC](https://www.ruby-forum.com/t/wrapping-a-structure-as-a-new-class/106285/3 "2007-06-11T05:11:44Z")

</div>

Hi,

At Mon, 11 Jun 2007 04:28:33 +0900,  
trebor777 wrote in [ruby-talk:255046]:

> Arf^^… nobody answered, but it’s ok, I’ve solved my problem…

We had a development camp and RubyKaigi2007 last week.

> It’s just because, i didnt’ defined the initialize method as a singleton  
> method (other tutorials never told me to…, and it has always worked so…)  
> Anyway, it works now! Raaaah it was so simple xD

The initialize method must be an instance method. Rather  
you’ll need the allocator function. You should not override  
Class.new unless you have special reasons.

static VALUE  
bitmap\_s\_alloc(VALUE klass)  
{  
return Data\_Wrap\_Struct(klass, 0, SDL\_FreeSurface, 0);  
}

static VALUE  
VALUE bitmap\_init(int argc, VALUE \*argv, VALUE self)  
{  
SDL\_Surface \*new\_bitmap;

```
switch (argc) {
  case 1:

```

new\_bitmap = IMG\_Load(StringValueCStr(argv[0]));  
break;  
case 2: {  
unsigned int x = NUM2UINT(argv[0]), y = NUM2UINT(argv[1]);  
new\_bitmap = SDL\_CreateRGBSurface(SDL\_HWSURFACE, x, y, 32, 0, 0, 0,  
0);  
break;  
default:  
rb\_raise(rb\_eArgError, “wrong number of arguments (%d for 1 or 2)”,  
argc);  
}  
DATA\_PTR(self) = new\_bitmap;  
return self;  
}

void  
Init\_bitmap(void)  
{  
VALUE cBitmap = rb\_define\_class(“Bitmap”, rb\_cData);

```
rb_define_alloc_func(cBitmap, bitmap_s_alloc);
rb_define_method(cBitmap, "initialize", bitmap_init, -1);

```

}

> > I got an argument type error on the class methods…(a.width for example):  
> > "wrong argument type Bitmap "

The default allocator function would be used.

---

<div class="post-metadata">

**Author:** ![trebor777](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a9adbd/32.png) [@trebor777](https://www.ruby-forum.com/u/trebor777)\
**Post date:** [June 11, 2007, 5:13pm UTC](https://www.ruby-forum.com/t/wrapping-a-structure-as-a-new-class/106285/4 "2007-06-11T17:13:35Z")

</div>

Ahh 🙂 Domo arigato! 😛  
You’ve even rewritten my code! That’s great! really Thank you!

Sorry for the “nobody answered” i didn’t know about that camp… ^^"
