On Friday 25 June 2004 08:57, Dave Thomas wrote:
>
> How would you handle
>      a = /cat/
>      a.class = File
>      a.read
>
> It's a brave man that calls Guy 'wrong,' so I'm looking forward to
> seeing your implementation of this.

With some small changes to object.c and ruby.h, this code:

a = /cat/
a.class = File
a.read

...results in this error:

testclass.rb:5:in `read': wrong argument type File (expected File) (TypeError)
        from testclass.rb:5

Which is pretty solid, and now I can change the class of an object.  A patch 
against the Ruby CVS follows, for reference.

	Sean O'Dell



Index: object.c
===================================================================
RCS file: /src/ruby/object.c,v
retrieving revision 1.153
diff -r1.153 object.c
201a202,209
> VALUE
> rb_obj_class_set(obj, new_class)
>     VALUE obj, new_class;
> {
>     RBASIC(obj)->klass = new_class;
>     return new_class;
> }
>
2546a2555
>     rb_define_method(rb_mKernel, "class=", rb_obj_class_set, 1);
Index: ruby.h
===================================================================
RCS file: /src/ruby/ruby.h,v
retrieving revision 1.105
diff -r1.105 ruby.h
415,420c415,420
< #define RMODULE(obj) RCLASS(obj)
< #define RFLOAT(obj)  (R_CAST(RFloat)(obj))
< #define RSTRING(obj) (R_CAST(RString)(obj))
< #define RREGEXP(obj) (R_CAST(RRegexp)(obj))
< #define RARRAY(obj)  (R_CAST(RArray)(obj))
< #define RHASH(obj)   (R_CAST(RHash)(obj))
---
> #define RMODULE(obj) (Check_Type(obj, T_MODULE), RCLASS(obj))
> #define RFLOAT(obj)  (Check_Type(obj, T_FLOAT), R_CAST(RFloat)(obj))
> #define RSTRING(obj) (Check_Type(obj, T_STRING), R_CAST(RString)(obj))
> #define RREGEXP(obj) (Check_Type(obj, T_REGEXP), R_CAST(RRegexp)(obj))
> #define RARRAY(obj)  (Check_Type(obj, T_ARRAY), R_CAST(RArray)(obj))
> #define RHASH(obj)   (Check_Type(obj, T_HASH), R_CAST(RHash)(obj))
422,424c422,424
< #define RSTRUCT(obj) (R_CAST(RStruct)(obj))
< #define RBIGNUM(obj) (R_CAST(RBignum)(obj))
< #define RFILE(obj)   (R_CAST(RFile)(obj))
---
> #define RSTRUCT(obj) (Check_Type(obj, T_STRUCT), R_CAST(RStruct)(obj))
> #define RBIGNUM(obj) (Check_Type(obj, T_BIGNUM), R_CAST(RBignum)(obj))
> #define RFILE(obj)   (Check_Type(obj, T_FILE), R_CAST(RFile)(obj))