Skip to content

Commit df61e2a

Browse files
committed
* struct.c (rb_struct_hash): new methods Struct#hash, Struct#eql?.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3683 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent d79a04f commit df61e2a

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Tue Apr 15 19:12:21 2003 Nobuyoshi Nakada <nobu.nokada@softhome.net>
2+
3+
* struct.c (rb_struct_hash): new methods Struct#hash, Struct#eql?.
4+
15
Tue Apr 15 16:05:11 2003 Nobuyoshi Nakada <nobu.nokada@softhome.net>
26

37
* numeric.c (rb_fix2str): buffer was insufficient.

struct.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,22 @@ rb_struct_equal(s, s2)
576576
return Qtrue;
577577
}
578578

579+
static VALUE
580+
rb_struct_hash(s)
581+
VALUE s;
582+
{
583+
long i, h;
584+
VALUE n;
585+
586+
h = rb_hash(rb_obj_class(s));
587+
for (i = 0; i < RSTRUCT(s)->len; i++) {
588+
h = (h << 1) | (h<0 ? 1 : 0);
589+
n = rb_hash(RSTRUCT(s)->ptr[i]);
590+
h ^= NUM2LONG(n);
591+
}
592+
return LONG2FIX(h);
593+
}
594+
579595
static VALUE
580596
rb_struct_size(s)
581597
VALUE s;
@@ -596,6 +612,8 @@ Init_Struct()
596612
rb_define_method(rb_cStruct, "copy_object", rb_struct_copy_object, 1);
597613

598614
rb_define_method(rb_cStruct, "==", rb_struct_equal, 1);
615+
rb_define_method(rb_cStruct, "eql?", rb_struct_equal, 1);
616+
rb_define_method(rb_cStruct, "hash", rb_struct_hash, 0);
599617

600618
rb_define_method(rb_cStruct, "to_s", rb_struct_to_s, 0);
601619
rb_define_method(rb_cStruct, "inspect", rb_struct_inspect, 0);

0 commit comments

Comments
 (0)