# Class object creation and scope

**URL:** <https://www.ruby-forum.com/t/class-object-creation-and-scope/50509>\
**Category:** Ruby\
**Created:** [December 17, 2005, 5:32am UTC](https://www.ruby-forum.com/t/class-object-creation-and-scope/50509 "2005-12-17T05:32:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [December 17, 2005, 5:32am UTC](https://www.ruby-forum.com/t/class-object-creation-and-scope/50509/1 "2005-12-17T05:32:29Z")

</div>

It appears that class objects (created via Class.new) are somewhat  
unusual.  
I’m trying to write some code to inject some non-Ruby data into these  
objects. I’m worried about what happens at GC time, so I need to use  
Data\_Wrap\_Struct() to create the class objects so that I can register my  
memory cleanup functions with Ruby.

However, this technique doesn’t seem to work. If I try to create a class  
object using:

Data\_Wrap\_Struct(rb\_cClass, 0, ReleaseClassObject, my\_data);

Ruby complains with a bizarre TypeError exception: “wrong argument type  
Class (expected Class)”.

I’ve tried some other workarounds involving the use of  
rb\_define\_alloc\_func(), which works fine for non class objects, but  
there  
seems to be something special about how Ruby handles class objects.

A secondary question: what’s the lifetime of a class object in Ruby? Are  
these things ever garbage collected?

Thanks  
-John

> **[John Lam's Blog](https://www.iunknown.com/)**
>
> Since 1997

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [December 17, 2005, 6:11am UTC](https://www.ruby-forum.com/t/class-object-creation-and-scope/50509/2 "2005-12-17T06:11:32Z")

</div>

I just took a look at rb\_class\_boot(), and it looks like I can’t make a  
class object a T\_DATA type. Ugh.

So it looks like my only hope is that class objects are never GC’d …  
pointers here would be super-helpful.

Thanks  
-John

> **[John Lam's Blog](https://www.iunknown.com)**
>
> Since 1997
