Class:Hash

Slevin McGuigan <[email protected]> Sun, 5 Dec 2010 22:29:34 +0100
Newsgroups gmane.comp.lang.ruby.documentation
Message-ID <[email protected]>
--0016e65a0dfcbc10320496b077ec
Content-Type: text/plain; charset=ISO-8859-1

Hi *,

I am new to ruby and new to this mailinglist. I hope my question isn't too
simple to be coped with. But as I want to get involved in the ruby community
I choosed this one as a lightweight issue to start with.

I stumbled about the behaviour of Hash#key (which is a strange method
anyway, imho. I guess I won't ever use it). The behavior of #key isn't
obvious in case there is more then one occurance of the key

testhash = {
  '1' => 'a',
  '2' => 'b',
  '3' => 'a'
}
puts "#{testhash.key('a')}"         # => 1
testhash.delete('1')
puts "#{testhash.key('a')}"         # => 3

http://rdoc.info/docs/ruby-core/1.9.2/Hash sais that "Hashes enumerate their
values in the order that the corresponding keys were inserted" Hence the
behavior is following the documentation (or vice versa)

But both http://rdoc.info/docs/ruby-core/1.9.2/Hash#key-instance_method and
http://www.ruby-doc.org/ruby-1.9/classes/Hash.html#M000387 only say "Returns
the key for a given value. If not found, returns nil".

And question #1 is: What is the best place too look/the official
documentation? And is this mailing list the proper way to discuss
documentation flaws?
And question #1 is: Shouldn't there be something in the documentation of
hash#key like "... If there is more then one occurance of the key the first
value is returned"

Best Regards,
Slevin

--

My system: Ubuntu 9.10, ruby 1.9.1
egon@egon:/home/egon> uname -a
Linux egon 2.6.31-22-generic #68-Ubuntu SMP Tue Oct 26 16:38:35 UTC 2010
i686 GNU/Linux
egon@egon:/home/egon> cat /etc/lsb\-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=9.10
DISTRIB_CODENAME=karmic
DISTRIB_DESCRIPTION="Ubuntu 9.10"
egon@egon:/home/egon> ruby -v
ruby 1.9.1p243 (2009-07-16 revision 24175) [i486-linux]

--0016e65a0dfcbc10320496b077ec
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hi *,<br><br>I am new to ruby and new to this mailinglist. I hope my questi=
on isn&#39;t too simple to be coped with. But as I want to get involved in =
the ruby community I choosed this one as a lightweight issue to start with.=
<br>
<br>I stumbled about the behaviour of Hash#key (which is a strange method a=
nyway, imho. I guess I won&#39;t ever use it). The behavior of #key isn&#39=
;t obvious in case there is more then one occurance of the key<br><br><span=
 style=3D"font-size: small;">testhash =3D {<br>
=A0 &#39;1&#39; =3D&gt; &#39;a&#39;,<br>=A0 &#39;2&#39; =3D&gt; &#39;b&#39;=
,<br>=A0 &#39;3&#39; =3D&gt; &#39;a&#39;<br>}<br>puts &quot;#{testhash.key(=
&#39;a&#39;)}&quot;=A0=A0=A0=A0=A0=A0=A0=A0 # =3D&gt; 1<br>testhash.delete(=
&#39;1&#39;)<br>puts &quot;#{testhash.key(&#39;a&#39;)}&quot;=A0=A0=A0=A0=
=A0=A0=A0=A0 # =3D&gt; 3</span><br>
<br><a href=3D"http://rdoc.info/docs/ruby-core/1.9.2/Hash">http://rdoc.info=
/docs/ruby-core/1.9.2/Hash</a> sais that &quot;Hashes enumerate their value=
s in the order that the corresponding keys were inserted&quot; Hence the be=
havior is following the documentation (or vice versa)<br>
<br>But both <a href=3D"http://rdoc.info/docs/ruby-core/1.9.2/Hash#key-inst=
ance_method">http://rdoc.info/docs/ruby-core/1.9.2/Hash#key-instance_method=
</a> and <a href=3D"http://www.ruby-doc.org/ruby-1.9/classes/Hash.html#M000=
387">http://www.ruby-doc.org/ruby-1.9/classes/Hash.html#M000387</a> only sa=
y &quot;Returns the key for a given value. If not found, returns nil&quot;.=
<br>
<br>And question #1 is: What is the best place too look/the official docume=
ntation? And is this mailing list the proper way to discuss documentation f=
laws?<br>And question #1 is: Shouldn&#39;t there be something in the docume=
ntation of hash#key like &quot;... If there is more then one occurance of t=
he key the first value is returned&quot;<br>
<br>Best Regards,<br>Slevin<br><br>--<br><br>My system: Ubuntu 9.10, ruby 1=
.9.1<br>egon@egon:/home/egon&gt; uname -a<br>Linux egon 2.6.31-22-generic #=
68-Ubuntu SMP Tue Oct 26 16:38:35 UTC 2010 i686 GNU/Linux<br>egon@egon:/hom=
e/egon&gt; cat /etc/lsb\-release<br>
DISTRIB_ID=3DUbuntu<br>DISTRIB_RELEASE=3D9.10<br>DISTRIB_CODENAME=3Dkarmic<=
br>DISTRIB_DESCRIPTION=3D&quot;Ubuntu 9.10&quot;<br>egon@egon:/home/egon&gt=
; ruby -v<br>ruby 1.9.1p243 (2009-07-16 revision 24175) [i486-linux]<br>

--0016e65a0dfcbc10320496b077ec--