Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
proposal Hash eql? , ==, ===
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
walter  
View profile  
 More options Apr 16 2004, 9:13 am
Newsgroups: comp.lang.ruby
From: wal...@mwsewall.com
Date: Fri, 16 Apr 2004 22:12:45 +0900
Local: Fri, Apr 16 2004 9:12 am
Subject: proposal Hash eql? , ==, ===
(Note:  I sent a similar email last night in response to Matz, but
did not see it.  I apologize if this is basically a duplicate.)

Hash#== should be for would be content equals and not
content and default value/proc equal. I think that most common case
for == would be content equality.

Hash#=== as Nobu pointed out === would make more sense to be a
membership test, i.e., alias for Hash#key?.

Hash.eql? should be content equal and default value/block (i.e what
the current == is now).  There is some precedence for this in Numeric
where 1 == 1.0 is true but 1.eql?(1.0) is false.  I see this as being
very similar circumstance.

Thanks for listening,

Walt

*****************************************************
Walter Szewelanczyk
IS Director
M.W. Sewall & CO.        email : wal...@mwsewall.com    
259 Front St.            Phone : (207) 442-7994 x 128
Bath, ME 04530           Fax   : (207) 443-6284
*****************************************************


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Yukihiro Matsumoto  
View profile  
 More options Apr 19 2004, 4:47 am
Newsgroups: comp.lang.ruby
From: m...@ruby-lang.org (Yukihiro Matsumoto)
Date: Mon, 19 Apr 2004 17:46:40 +0900
Local: Mon, Apr 19 2004 4:46 am
Subject: Re: proposal Hash eql? , ==, ===
Hi,

In message "proposal Hash eql? , ==, ==="
    on 04/04/16, wal...@mwsewall.com <wal...@mwsewall.com> writes:

|Hash#== should be for would be content equals and not
|content and default value/proc equal. I think that most common case
|for == would be content equality.
|
|Hash#=== as Nobu pointed out === would make more sense to be a
|membership test, i.e., alias for Hash#key?.
|
|Hash.eql? should be content equal and default value/block (i.e what
|the current == is now).  There is some precedence for this in Numeric
|where 1 == 1.0 is true but 1.eql?(1.0) is false.  I see this as being
|very similar circumstance.

I agree with both Hash#== and Hash#eql?  But I'm not very positive
about making Hash#=== a membership test.  Some usage of "===" are
membership, for example, Array and Class, but they are not general
consensus.  I'm not sure how much useful that making === membership.

                                                        matz.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Elias Athanasopoulos  
View profile  
 More options Apr 19 2004, 6:32 am
Newsgroups: comp.lang.ruby
From: Elias Athanasopoulos <elat...@phys.uoa.gr>
Date: Mon, 19 Apr 2004 19:32:07 +0900
Local: Mon, Apr 19 2004 6:32 am
Subject: Re: proposal Hash eql? , ==, ===

> I agree with both Hash#== and Hash#eql?  But I'm not very positive

elathan@testbed:~/hacking/ruby> cat ../test.rb

h1 = Hash.new(42)
h1["a"] = 1
h1["b"] = 2

h2 = { "a" => 1, "b" => 2 }

puts h1 == h2
puts h1.eql?(h2)
elathan@testbed:~/hacking/ruby> ./ruby ../test.rb
true
false

--- hash.c.orig 2004-04-19 12:08:24.000000000 +0300
+++ hash.c      2004-04-19 12:21:06.000000000 +0300
@@ -1425,6 +1425,48 @@
     }
     if (RHASH(hash1)->tbl->num_entries != RHASH(hash2)->tbl->num_entries)
        return Qfalse;
+
+    data.tbl = RHASH(hash2)->tbl;
+    data.result = Qtrue;
+    st_foreach(RHASH(hash1)->tbl, equal_i, (st_data_t)&data);
+
+    return data.result;
+}
+
+/*
+ *  call-seq:
+ *     hsh.eql? other_hash    => true or false
+ *
+ *  Strict Equality---Two hashes are strictly equal if they each contain  
+ *  the same number of keys and if each key-value pair is equal to (according to
+ *  <code>Object#==</code>) the corresponding elements in the other
+ *  hash. Both hashes must have equal default values/procs.
+ *      
+ *     h1 = { "a" => 1, "c" => 2 }
+ *     h2 = { 7 => 35, "c" => 2, "a" => 1 }
+ *     h3 = { "a" => 1, "c" => 2, 7 => 35 }
+ *     h4 = { "a" => 1, "d" => 2, "f" => 35 }
+ *     h1.eql? h2   #=> false
+ *     h2.eql? h3   #=> true
+ *     h3.eql? h4   #=> false
+ *      
+ */
+
+static VALUE
+rb_hash_strict_equal(hash1, hash2)
+    VALUE hash1, hash2;
+{
+    struct equal_data data;
+
+    if (hash1 == hash2) return Qtrue;
+    if (TYPE(hash2) != T_HASH) {
+       if (!rb_respond_to(hash2, rb_intern("to_hash"))) {
+           return Qfalse;
+       }
+       return rb_equal(hash2, hash1);
+    }
+    if (RHASH(hash1)->tbl->num_entries != RHASH(hash2)->tbl->num_entries)
+       return Qfalse;
     if (!(rb_equal(RHASH(hash1)->ifnone, RHASH(hash2)->ifnone) &&
          FL_TEST(hash1, HASH_PROC_DEFAULT) == FL_TEST(hash2,
HASH_PROC_DEFAULT)))
        return Qfalse;
@@ -2365,6 +2407,8 @@
     rb_define_method(rb_cHash,"inspect", rb_hash_inspect, 0);

     rb_define_method(rb_cHash,"==", rb_hash_equal, 1);
+    rb_define_method(rb_cHash,"eql?", rb_hash_strict_equal, 1);
+    rb_define_method(rb_cHash,"==", rb_hash_equal, 1);
     rb_define_method(rb_cHash,"[]", rb_hash_aref, 1);
     rb_define_method(rb_cHash,"fetch", rb_hash_fetch, -1);
     rb_define_method(rb_cHash,"[]=", rb_hash_aset, 2);


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »