Re: Class:Hash
Slevin McGuigan <[email protected]> Sun, 12 Dec 2010 14:32:43 +0100
| Newsgroups | gmane.comp.lang.ruby.documentation |
|---|---|
| Message-ID | <[email protected]> |
--0016367f9e024eea1f0497369fc4 Content-Type: text/plain; charset=ISO-8859-1 Hi Bernado, thanks for your input. Unfortunately my question was not about getting values by keys. My issue concerns acessing keys by values (which is, I know, a very seldom use of hashes). This is done by using the #key method or by using the #index method (deprecated in v1.9) Although chances are low that #key is used often, Class hash does offer this method. Hence I guess it should be working correctly (which it does imho) and it should be documented correctly. This is what I wanted to discuss/clarify in this thread. Any more opinions on the documentation of #key? Shouldn't there be something in the documentation of hash#key like "... If there is more then one occurance of the value the first key is returned" (addendum: in my first eMail I mixed value and key. I just corrected this here) Thanks and with best Regards, Slevin 2010/12/9 bernardo galindo <[email protected]> > "Hashes have a *default value* that is returned when accessing keys that > do not exist in the hash. By default, that value is nil." from > http://rdoc.info/docs/ruby-core/1.9.2/Hash > so if you type something like this: > ---entry----- return > testhash["1"] => "a" because the key "1" exist and has value "a" > > if you type some like this: > testhash["a"] => nil according with the doc; the hash method is > looking for the key "a", but this doesn't exist in the hash, so the > method return the default value => nil. > > If you want to get all the keys: > result = testhash.keys return an array of all keys that exist > > > if you want to know if a specific key exists: > testhash.key?("1") return true > testhash.key?("a") return false > > I think that http://rdoc.info/docs/ruby-core/1.9.2/Hash is good resource, > These pages are some courses for learn ruby: > http://tryruby.org/ with out install ruby > http://rubykoans.com/ for learn ruby > > Eng'r. Bernardo Galindo > Ruby on Rails developer > Frontend developer > at CrowdInteractive => www.crowdint.com > member of @rubyc_ => ruby community, ruby colima > > @bernardo466 > > 2010/12/5 Slevin McGuigan <[email protected]> > > 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_methodand >> 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 #2 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] >> > > --0016367f9e024eea1f0497369fc4 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi Bernado,<br><br>thanks for your input. Unfortunately my question was not= about getting values by keys. My issue concerns acessing keys by values (w= hich is, I know, a very seldom use of hashes). This is done by using the #k= ey method or by using the #index method (deprecated in v1.9)<br> <br>Although chances are low that #key is used often, Class hash does offer= this method. Hence I guess it should be working correctly (which it does i= mho) and it should be documented correctly. This is what I wanted to discus= s/clarify in this thread.<br> <br>Any more opinions on the documentation of #key? Shouldn't there be = something in the documentation of hash#key like "... If there is more then one occurance of the value t= he=20 first key is returned"=A0=A0=A0 (addendum: in my first eMail I mixed v= alue and key. I just corrected this here)<br><br>Thanks and with best Regar= ds,<br>Slevin<br><br><br><br><div class=3D"gmail_quote">2010/12/9 bernardo = galindo <span dir=3D"ltr"><<a href=3D"mailto:[email protected]">bern= [email protected]</a>></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;"><span style=3D"fo= nt-family: 'Lucida Sans','Lucida Grande',Verdana,Arial,sans= -serif; font-size: 13px;">"Hashes have a=A0<i>default value</i>=A0that= is returned when accessing keys that do not exist in the hash. By default,= that value is=A0<tt>nil</tt>." from=A0</span><a href=3D"http://rdoc.i= nfo/docs/ruby-core/1.9.2/Hash" target=3D"_blank">http://rdoc.info/docs/ruby= -core/1.9.2/Hash</a>=A0<div> so if you type something like this:</div><div>=A0=A0---entry----- =A0 =A0 = =A0 =A0 =A0 =A0return</div><div>=A0testhash["1"] =A0=3D> =A0 = =A0"a" =A0 =A0 because the key "1" exist and has=A0valu= e=A0"a"=A0</div><div> <br></div><div>if you type some like this:</div><div>=A0testhash["a&qu= ot;] =A0 =3D> =A0nil =A0 =A0=A0=A0according with the doc; the hash metho= d is =A0looking for the =A0key "a", =A0 but this doesn't exis= t in the hash, so the method =A0return the default value =3D> nil.</div> <div><br></div><div>If you want to get all the keys:</div><div>result =3D t= esthash.keys =A0 return an array of all keys that exist</div><div><br></div= ><div><br></div><div>if you want to know if a specific key exists:</div><di= v> testhash.key?("1") =A0return true</div><div>testhash.key?("a= ") return false</div><div><br></div><div>I think that <a href=3D"http:= //rdoc.info/docs/ruby-core/1.9.2/Hash" target=3D"_blank">http://rdoc.info/d= ocs/ruby-core/1.9.2/Hash</a> =A0is good resource, These pages are some cour= ses for learn ruby:</div> <div><span style=3D"font-family: Arial,'Helvetica Neue',sans-serif;= font-size: 15px; color: rgb(68, 68, 68); line-height: 19px;"><a href=3D"ht= tp://tryruby.org/" rel=3D"nofollow" title=3D"http://tryruby.org/" style=3D"= margin: 0px; padding: 0px; border-width: 0px; color: rgb(0, 132, 180); text= -decoration: underline;" target=3D"_blank">http://tryruby.org/</a></span><s= pan style=3D"font-family: Arial,'Helvetica Neue',sans-serif; font-s= ize: 15px; color: rgb(68, 68, 68); line-height: 19px;">=A0with out install = ruby</span></div> <div><font color=3D"#444444" face=3D"Arial, 'Helvetica Neue', sans-= serif" size=3D"4"><span style=3D"font-size: 15px; line-height: 19px;"><a hr= ef=3D"http://rubykoans.com/" rel=3D"nofollow" title=3D"http://rubykoans.com= /" style=3D"margin: 0px; padding: 0px; border-width: 0px; color: rgb(0, 132= , 180); text-decoration: underline;" target=3D"_blank">http://rubykoans.com= /</a>=A0for learn ruby</span></font></div> <div><font color=3D"#444444" face=3D"Arial, 'Helvetica Neue', sans-= serif" size=3D"4"><span style=3D"font-size: 15px; line-height: 19px;"><br><= /span></font></div><div><font color=3D"#444444" face=3D"Arial, 'Helveti= ca Neue', sans-serif" size=3D"4"><span style=3D"font-size: 15px; line-h= eight: 19px;"><span style=3D"color: rgb(0, 0, 0); font-family: Verdana,sans= -serif; line-height: 16px; font-size: 12px;">Eng'r. Bernardo Galindo</s= pan></span></font></div> <div><font color=3D"#444444" face=3D"Arial, 'Helvetica Neue', sans-= serif" size=3D"4"><span style=3D"font-size: 15px; line-height: 19px;"><font= color=3D"#000000" face=3D"Verdana, sans-serif" size=3D"3"><span style=3D"f= ont-size: 12px; line-height: 16px;">Ruby on Rails developer</span></font></= span></font></div> <div><font color=3D"#444444" face=3D"Arial, 'Helvetica Neue', sans-= serif" size=3D"4"><span style=3D"font-size: 15px; line-height: 19px;"><font= color=3D"#000000" face=3D"Verdana, sans-serif" size=3D"3"><span style=3D"f= ont-size: 12px; line-height: 16px;">Frontend developer</span></font></span>= </font></div> <div><font color=3D"#444444" face=3D"Arial, 'Helvetica Neue', sans-= serif" size=3D"4"><span style=3D"font-size: 15px; line-height: 19px;"><font= color=3D"#000000" face=3D"Verdana, sans-serif" size=3D"3"><span style=3D"f= ont-size: 12px; line-height: 16px;">at CrowdInteractive =3D> <a href=3D"= http://www.crowdint.com" target=3D"_blank">www.crowdint.com</a></span></fon= t></span></font></div> <div><font color=3D"#444444" face=3D"Arial, 'Helvetica Neue', sans-= serif" size=3D"4"><span style=3D"font-size: 15px; line-height: 19px;"><font= color=3D"#000000" face=3D"Verdana, sans-serif" size=3D"3"><span style=3D"f= ont-size: 12px; line-height: 16px;">member of @rubyc_ =3D> ruby communit= y, ruby colima</span></font></span></font></div> <div><font color=3D"#444444" face=3D"Arial, 'Helvetica Neue', sans-= serif" size=3D"4"><span style=3D"font-size: 15px; line-height: 19px;"><font= color=3D"#000000" face=3D"Verdana, sans-serif" size=3D"3"><span style=3D"f= ont-size: 12px; line-height: 16px;"><br> </span></font></span></font></div><div><font color=3D"#444444" face=3D"Aria= l, 'Helvetica Neue', sans-serif" size=3D"4"><span style=3D"font-siz= e: 15px; line-height: 19px;"><font color=3D"#000000" face=3D"Verdana, sans-= serif" size=3D"3"><span style=3D"font-size: 12px; line-height: 16px;">@bern= ardo466<br> </span></font></span></font><br><div class=3D"gmail_quote">2010/12/5 Slevin= McGuigan <span dir=3D"ltr"><<a href=3D"mailto:slevin.mcguigan@googlemai= l.com" target=3D"_blank">[email protected]</a>></span><div>= <div> </div><div class=3D"h5"><br><blockquote class=3D"gmail_quote" style=3D"marg= in: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-l= eft: 1ex;"> Hi *,<br><br>I am new to ruby and new to this mailinglist. I hope my questi= on 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.= <br> <br>I stumbled about the behaviour of Hash#key (which is a strange method a= nyway, 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<br><br> <span style=3D"font-size: small;">testhash =3D {<br> =A0 '1' =3D> 'a',<br>=A0 '2' =3D> 'b'= ,<br>=A0 '3' =3D> 'a'<br>}<br>puts "#{testhash.key(= 'a')}"=A0=A0=A0=A0=A0=A0=A0=A0 # =3D> 1<br>testhash.delete(= '1')<br>puts "#{testhash.key('a')}"=A0=A0=A0=A0= =A0=A0=A0=A0 # =3D> 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 "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 "Returns the key for a given val= ue. If not found, returns nil".<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 #2 is: Shouldn't there be something in the docume= ntation of hash#key like "... If there is more then one occurance of t= he key the first value is returned"<br> <br>Best Regards,<br>Slevin<br><br>--<br><br>My system: Ubuntu 9.10, ruby 1= .9.1<br>egon@egon:/home/egon> 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> cat /etc/lsb\-release<br> DISTRIB_ID=3DUbuntu<br>DISTRIB_RELEASE=3D9.10<br>DISTRIB_CODENAME=3Dkarmic<= br>DISTRIB_DESCRIPTION=3D"Ubuntu 9.10"<br>egon@egon:/home/egon>= ; ruby -v<br>ruby 1.9.1p243 (2009-07-16 revision 24175) [i486-linux]<br> </blockquote></div></div></div><br></div> </blockquote></div><br> --0016367f9e024eea1f0497369fc4--