summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-01-23 09:34:04 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-01-23 09:34:04 +0000
commitaaae2e4765ef09f09490978f62e64b69f597ad1d (patch)
tree74b5b7b6fb7c4bff676933d02e05304db549bb91
parent46e8b340df47aa63d60643deaf98e8209b2e1540 (diff)
* hash.c: added documentation for Hash about how it uses eql? and
hash methods for the keys. [ruby-core:09995] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@11567 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--hash.c34
2 files changed, 38 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index c39e5958f1..2643f452cd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,11 @@ Tue Jan 23 18:26:12 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* lib/cgi.rb (CGI::QueryExtension::read_multipart): use == instead
of ===. [ruby-dev:30176]
+Tue Jan 23 10:48:17 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
+
+ * hash.c: added documentation for Hash about how it uses eql? and
+ hash methods for the keys. [ruby-core:09995]
+
Mon Jan 22 14:57:25 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* ext/socket/socket.c: fix errors in socket sample code.
diff --git a/hash.c b/hash.c
index 0d4d43315b..e2f7345f5a 100644
--- a/hash.c
+++ b/hash.c
@@ -2414,7 +2414,39 @@ env_update(env, hash)
* Hashes have a <em>default value</em> that is returned when accessing
* keys that do not exist in the hash. By default, that value is
* <code>nil</code>.
- *
+ *
+ * <code>Hash</code> uses <code>key.eql?</code> to test keys for equality.
+ * If you need to use instances of your own classes as keys in a <code>Hash</code>,
+ * it is recommended that you define both the <code>eql?</code> and <code>hash</code>
+ * methods. The <code>hash</code> method must have the property that
+ * <code>a.eql?(b)</code> implies <code>a.hash == b.hash</code>.
+ *
+ * class MyClass
+ * attr_reader :str
+ * def initialize(str)
+ * @str = str
+ * end
+ * def eql?(o)
+ * o.is_a?(MyClass) && str == o.str
+ * end
+ * def hash
+ * @str.hash
+ * end
+ * end
+ *
+ * a = MyClass.new("some string")
+ * b = MyClass.new("some string")
+ * a.eql? b #=> true
+ *
+ * h = {}
+ *
+ * h[a] = 1
+ * h[a] #=> 1
+ * h[b] #=> 1
+ *
+ * h[b] = 2
+ * h[a] #=> 2
+ * h[b] #=> 2
*/
void