Forum: Ruby A tip about using Hash - and a documentation request

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7e483d1e04ad24c9a8e0612ed14e8f72?d=identicon&s=25 Per Velschow (pervel)
on 2007-01-22 06:16
I just spent several hours of investigating a problem that I had when
using instances of my own class as keys in a Hash. If you, like me, have
experience with this from a language like Java, you know that you have
to override both the hash method and the equality operator/method. What
took me by surprise was that Hash does NOT use the == operator when
comparing keys. Instead, it uses the eql? method. Thus, it is the eql?
method you have to define for your classes.

The above is actually documented in the Ruby core docs - but not in the
most obvious place in my opinion. It is mentioned only in the
Object.hash documentation. I never considered looking there for the
answer - I only looked at the docs for Hash and it makes no mention of
this fact. This may be my fault. But in my opinion this behavior is a
feature of the Hash class first and foremost. As such it is rather
important to document it in the Hash class.

/Per

P.S. One reason it took me so long to figure out is that Googling for
words like "hash", "key", etc. is not very specific and gives way too
many unrelated results. :)
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2007-01-22 08:44
(Received via mailing list)
On Jan 21, 2007, at 21:16, Per Velschow wrote:

> The above is actually documented in the Ruby core docs - but not in
> the
> most obvious place in my opinion. It is mentioned only in the
> Object.hash documentation. I never considered looking there for the
> answer - I only looked at the docs for Hash and it makes no mention of
> this fact. This may be my fault. But in my opinion this behavior is a
> feature of the Hash class first and foremost. As such it is rather
> important to document it in the Hash class.

Please provide a patch to the ruby-doc mailing list, or to ruby-core
and I'll commit it.  You'll find the RDoc for Hash in hash.c in a
ruby tarball (or SVN), right above Init_Hash.  It'll be easiest to
apply from trunk or ruby_1_8.

ruby-doc@ruby-lang.org

ruby-core@ruby-lang.org

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!
7e483d1e04ad24c9a8e0612ed14e8f72?d=identicon&s=25 Per Velschow (pervel)
on 2007-01-22 10:49
I've never made a patch before, so I decided to follow the instructions
given at http://www.ruby-lang.org/en/community/ruby-core/ (up until
point 3). So I have a patch file now and I have run make and make test
with success. I had little success trying to install the docs using make
install-doc though. It creates yaml files only that I don't know what to
do with.

So how do you want me to proceed? I have the patch file. I could login
and submit it to the tracker as explained in the instructions from
above. Or do you want me to sign up to one of the mailing lists you
mentioned and post it there?
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2007-01-22 11:09
(Received via mailing list)
On Jan 22, 2007, at 01:49, Per Velschow wrote:

> I've never made a patch before, so I decided to follow the
> instructions
> given at http://www.ruby-lang.org/en/community/ruby-core/ (up until
> point 3). So I have a patch file now and I have run make and make test
> with success. I had little success trying to install the docs using
> make
> install-doc though. It creates yaml files only that I don't know
> what to
> do with.

After installing the documentation you should be able to

ri Hash

and see your new documentation.

You should also be able to run:

rdoc --op rdoc hash.c

then open rdoc/index.html in your browser to find your RDoc.

> So how do you want me to proceed? I have the patch file. I could login
> and submit it to the tracker as explained in the instructions from
> above. Or do you want me to sign up to one of the mailing lists you
> mentioned and post it there?

Tracker works great too.  Assign it to me, if the tracker allows you
to.  It may be a while before I get around to committing it.

Thanks!

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2007-01-22 11:12
(Received via mailing list)
On Jan 22, 2007, at 1:49 AM, Per Velschow wrote:

> I've never made a patch before, so I decided to follow the
> instructions
> given at http://www.ruby-lang.org/en/community/ruby-core/ (up until
> point 3). So I have a patch file now and I have run make and make test
> with success. I had little success trying to install the docs using
> make
> install-doc though. It creates yaml files only that I don't know
> what to
> do with.

For the record... You RULE.
7e483d1e04ad24c9a8e0612ed14e8f72?d=identicon&s=25 Per Velschow (pervel)
on 2007-01-22 12:43
OK, I submitted it to the tracker. It was not possible for me to assign
it. I made a note of it in the description instead.
This topic is locked and can not be replied to.