Re: Class:Hash

Slevin McGuigan <[email protected]> Wed, 8 Dec 2010 15:56:00 +0100
Newsgroups gmane.comp.lang.ruby.documentation
Message-ID <[email protected]>
--0016e6d77e5ec265bb0496e75183
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Hi,

thanks, this helps (partially). This documentation/piece of code shows that
in case there are duplicate values the _first_ value is relevant (for
Hash#invert). This fits into my current understanding, Hash#index respectiv=
e
Hash#key do it the the same way.

Unfortunately my question #1 has not been answered. What/where is the
official ruby documentation? rdoc.info or ruby-doc.org? You point to
ruby-doc.org, hence I assume you vote for ruby-doc.org?

~~~

Further on question #2: Shouldn't there be something in the documentation o=
f
hash#key like "... If there is more then one occurance of the value the
first key is returned" [EDIT: I just noticed that I mixed key and value in
my first email]

What are your opinions about this?

Thanks and with best regards,
Slevin


2010/12/8 =E5=AE=8B=E5=8D=AB <[email protected]>

> my ruby version is 1.8.7
> may be you can view
> http://www.ruby-doc.org/core-1.8.7/index.html
>
> i found this :
> --------------------------------------------------
> Returns a new hash created by using hsh=E2=80=98s values as keys, and the=
 keys as
> values.
>
> h =3D { "n" =3D> 100, "m" =3D> 100, "y" =3D> 300, "d" =3D> 200, "a" =3D> =
0 }
> h.invert #=3D> {0=3D>"a", 100=3D>"n", 200=3D>"d", 300=3D>"y"}
> ----------------------------------------------------------
> maybe you can find your answer.
>
>
> At 2010-12-06 05:29:34=EF=BC=8C"Slevin McGuigan" <slevin.mcguigan@googlem=
ail.com>
> wrote:
>
> Hi *,
>
> I am new to ruby and new to this mailinglist. I hope my question isn't to=
o
> simple to be coped with. But as I want to get involved in the ruby commun=
ity
> 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 =3D {
>   '1' =3D> 'a',
>   '2' =3D> 'b',
>   '3' =3D> 'a'
> }
> puts "#{testhash.key('a')}"         # =3D> 1
> testhash.delete('1')
> puts "#{testhash.key('a')}"         # =3D> 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" Henc=
e
> the behavior is following the documentation (or vice versa)
>
> But both http://rdoc.info/docs/ruby-core/1.9.2/Hash#key-instance_methodan=
d
> 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 fir=
st
> 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=3DUbuntu
> DISTRIB_RELEASE=3D9.10
> DISTRIB_CODENAME=3Dkarmic
> DISTRIB_DESCRIPTION=3D"Ubuntu 9.10"
> egon@egon:/home/egon> ruby -v
> ruby 1.9.1p243 (2009-07-16 revision 24175) [i486-linux]
>
>
>
> ------------------------------
> =E7=BD=91=E6=98=93163/126=E9=82=AE=E7=AE=B1=E7=99=BE=E5=88=86=E7=99=BE=E5=
=85=BC=E5=AE=B9iphone ipad=E9=82=AE=E4=BB=B6=E6=94=B6=E5=8F=91<http://help.=
163.com/special/00753VB9/126mail_adv_guide.html?id=3D2840>

--0016e6d77e5ec265bb0496e75183
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Hi,<br><br>thanks, this helps (partially). This documentation/piece of code=
 shows that in case there are duplicate values the _first_ value is relevan=
t (for Hash#invert). This fits into my current understanding, Hash#index re=
spective Hash#key do it the the same way.<br>
<br>Unfortunately my question #1 has not been answered. What/where is the o=
fficial ruby documentation? <a href=3D"http://rdoc.info">rdoc.info</a> or <=
a href=3D"http://ruby-doc.org">ruby-doc.org</a>? You point to <a href=3D"ht=
tp://ruby-doc.org">ruby-doc.org</a>, hence I assume you vote for <a href=3D=
"http://ruby-doc.org">ruby-doc.org</a>?<br>
<br>~~~<br><br>Further on question #2: Shouldn&#39;t there be something in =
the documentation of hash#key like &quot;...=20
If there is more then one occurance of the value the first key is=20
returned&quot; [EDIT: I just noticed that I mixed key and value in my first=
 email]<br><br>What are your opinions about this?<br><br>Thanks and with be=
st regards,<br>Slevin<br><br><br><div class=3D"gmail_quote">2010/12/8 =E5=
=AE=8B=E5=8D=AB <span dir=3D"ltr">&lt;<a href=3D"mailto:[email protected]=
m">[email protected]</a>&gt;</span><br>
<blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde=
r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">my ruby version i=
s  1.8.7=20
<br>may be you can view=20
<br><a href=3D"http://www.ruby-doc.org/core-1.8.7/index.html" target=3D"_bl=
ank">http://www.ruby-doc.org/core-1.8.7/index.html</a>
<br>
<br>i found this :
<br>--------------------------------------------------
<br>Returns a new hash created by using hsh=E2=80=98s values as keys, and t=
he keys as values.=20
<br>
<br>   h =3D { &quot;n&quot; =3D&gt; 100, &quot;m&quot; =3D&gt; 100, &quot;=
y&quot; =3D&gt; 300, &quot;d&quot; =3D&gt; 200, &quot;a&quot; =3D&gt; 0 }
<br>   h.invert   #=3D&gt; {0=3D&gt;&quot;a&quot;, 100=3D&gt;&quot;n&quot;,=
 200=3D&gt;&quot;d&quot;, 300=3D&gt;&quot;y&quot;}
<br>----------------------------------------------------------
<br>maybe you can find your answer.<div><div></div><div class=3D"h5"><br><b=
r>At 2010-12-06 05:29:34=EF=BC=8C&quot;Slevin=C2=A0McGuigan&quot;=C2=A0&lt;=
<a href=3D"mailto:[email protected]" target=3D"_blank">slevin.=
[email protected]</a>&gt; wrote:<br>
 <blockquote style=3D"padding-left: 1ex; margin: 0px 0px 0px 0.8ex; border-=
left: 1px solid rgb(204, 204, 204);">Hi *,<br><br>I am new to ruby and new =
to this mailinglist. I hope my question isn&#39;t too simple to be coped wi=
th. 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>
=C2=A0 &#39;1&#39; =3D&gt; &#39;a&#39;,<br>=C2=A0 &#39;2&#39; =3D&gt; &#39;=
b&#39;,<br>=C2=A0 &#39;3&#39; =3D&gt; &#39;a&#39;<br>}<br>puts &quot;#{test=
hash.key(&#39;a&#39;)}&quot;=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0 # =3D&gt; 1<br>testhash.delete(&#39;1&#39;)<br>puts &quot;#{testhash.ke=
y(&#39;a&#39;)}&quot;=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 # =3D=
&gt; 3</span><br>

<br><a href=3D"http://rdoc.info/docs/ruby-core/1.9.2/Hash" target=3D"_blank=
">http://rdoc.info/docs/ruby-core/1.9.2/Hash</a> sais that &quot;Hashes enu=
merate their values in the order that the corresponding keys were inserted&=
quot; Hence the behavior 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" target=3D"_blank">http://rdoc.info/docs/ruby-core/1.9.2/Hash#k=
ey-instance_method</a> and <a href=3D"http://www.ruby-doc.org/ruby-1.9/clas=
ses/Hash.html#M000387" target=3D"_blank">http://www.ruby-doc.org/ruby-1.9/c=
lasses/Hash.html#M000387</a> only say &quot;Returns the key for a given val=
ue. 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>
</blockquote><br><br></div></div><span title=3D"neteasefooter"><span><hr>
<a href=3D"http://help.163.com/special/00753VB9/126mail_adv_guide.html?id=
=3D2840" target=3D"_blank">=E7=BD=91=E6=98=93163/126=E9=82=AE=E7=AE=B1=E7=
=99=BE=E5=88=86=E7=99=BE=E5=85=BC=E5=AE=B9iphone ipad=E9=82=AE=E4=BB=B6=E6=
=94=B6=E5=8F=91</a>
</span></span></blockquote></div><br>

--0016e6d77e5ec265bb0496e75183--