Skip to content

Commit

Permalink
* struct.c (rb_struct_hash): new methods Struct#hash, Struct#eql?.
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3683 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Apr 15, 2003
1 parent d79a04f commit df61e2a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
@@ -1,3 +1,7 @@
Tue Apr 15 19:12:21 2003 Nobuyoshi Nakada <nobu.nokada@softhome.net>

* struct.c (rb_struct_hash): new methods Struct#hash, Struct#eql?.

Tue Apr 15 16:05:11 2003 Nobuyoshi Nakada <nobu.nokada@softhome.net>

* numeric.c (rb_fix2str): buffer was insufficient.
Expand Down
18 changes: 18 additions & 0 deletions struct.c
Expand Up @@ -576,6 +576,22 @@ rb_struct_equal(s, s2)
return Qtrue;
}

static VALUE
rb_struct_hash(s)
VALUE s;
{
long i, h;
VALUE n;

h = rb_hash(rb_obj_class(s));
for (i = 0; i < RSTRUCT(s)->len; i++) {
h = (h << 1) | (h<0 ? 1 : 0);
n = rb_hash(RSTRUCT(s)->ptr[i]);
h ^= NUM2LONG(n);
}
return LONG2FIX(h);
}

static VALUE
rb_struct_size(s)
VALUE s;
Expand All @@ -596,6 +612,8 @@ Init_Struct()
rb_define_method(rb_cStruct, "copy_object", rb_struct_copy_object, 1);

rb_define_method(rb_cStruct, "==", rb_struct_equal, 1);
rb_define_method(rb_cStruct, "eql?", rb_struct_equal, 1);
rb_define_method(rb_cStruct, "hash", rb_struct_hash, 0);

rb_define_method(rb_cStruct, "to_s", rb_struct_to_s, 0);
rb_define_method(rb_cStruct, "inspect", rb_struct_inspect, 0);
Expand Down

0 comments on commit df61e2a

Please sign in to comment.