[ruby-talk:00343] Re: Struct.new

From: matz@... (Yukihiro Matsumoto)
Date: 1999-05-26 02:58:46 UTC
List: ruby-talk #343
Hi.

In message "[ruby-talk:00341] Struct.new"
    on 99/05/25, "Michael Neumann" <neumann@s-direktnet.de> writes:

|Following code is incompatible between 1.25 and 1.3x
|
|#version 1.25:
|St = Struct.new("St", :first, :second)
|s = St.new
|
|#version 1.3x:
|St = Struct.new("St", :first, :second)
|s = St.new    # error: `initialize': struct size differs (ArgumentError)
|a = St.new(3,4) #works
|
|Perhaps this could be changed?

Hmm, it's really easy to relax the member check (with included
patch).  But I want to discuss whether we should choose this behavior
or not.  What do you guys (other than Pros) think about this?

                                                matz.

--- struct.c	1999/05/25 08:26:17	1.1.1.3.2.6
+++ struct.c	1999/05/26 02:35:22
@@ -251,8 +251,12 @@
     size = iv_get(klass, "__size__");
     n = FIX2INT(size);
-    if (n != RARRAY(values)->len) {
+    if (n < RARRAY(values)->len) {
 	rb_raise(rb_eArgError, "struct size differs");
     }
     MEMCPY(RSTRUCT(self)->ptr, RARRAY(values)->ptr, VALUE, RARRAY(values)->len);
+    if (n > RARRAY(values)->len) {
+	rb_mem_clear(RSTRUCT(self)->ptr+RARRAY(values)->len,
+		     n-RARRAY(values)->len);
+    }
     return Qnil;
 }

In This Thread