On Monday, March 14, 2005, 6:19:24 PM, Yukihiro wrote:
> |Would this be useful to others? What would a good example be?
> Might be. Would someone in rdoc reference maintainers merge this if
> he feels this one valuable?
> matz.
I think it's valuable and would like someone to commit it, once an
example has been found. I think a good example is
class Details
attr_accessor :name, :age
def ==(other)
@name = other.name and @age = other.age
end
def hash
@name.to_s + "," + @age.to_s
end
end
A good rule of thumb (which should be included in the documentation):
if you define #== in a class, you should also define #hash. It's
"data" objects that typically need these, because they are the kinds
of things that will be stored in hashes.
Gavin
> |Index: hash.c
> |===================================================================
> |RCS file: /src/ruby/hash.c,v
> |retrieving revision 1.128.2.11
> |diff -u -r1.128.2.11 hash.c
> |--- hash.c 28 Feb 2005 02:45:20 -0000 1.128.2.11
> |+++ hash.c 13 Mar 2005 20:44:09 -0000
> |@@ -276,6 +276,18 @@
> | * h["d"] #=> "Go Fish: d"
> | * h.keys #=> ["c", "d"]
> | *
> |+ * == Being Hashable
> |+ *
> |+ * Hash calls the #hash method of key objects in order to determine
> identity,
> |+ * and insert them. Two objects sharing the same #hash value are considered
> |+ * to be identical keys, see Object#hash for more information.
> |+ *
> |+ * Certain builtin types such as Fixnum, String, and Symbol have their
> #hash
> |+ * methods called directly, so that redefining #hash for those
> |+ * types will have no effect.
> |+ *
> |+ * Would an example of how to define a useful #hash be useful here? The
> only
> |+ * one I can think of is for a case-insensitive String.
> | */
> |
> | static VALUE
|