Re: Class:Hash
bernardo galindo <[email protected]> Thu, 9 Dec 2010 00:21:33 -0600
| Newsgroups | gmane.comp.lang.ruby.documentation |
|---|---|
| Message-ID | <[email protected]> |
--0016367f9e02d4dbb40496f43f1d
Content-Type: text/plain; charset=ISO-8859-1
"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 #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]
>
--0016367f9e02d4dbb40496f43f1d
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<meta http-equiv=3D"content-type" content=3D"text/html; charset=3Dutf-8"><s=
pan class=3D"Apple-style-span" style=3D"font-family: 'Lucida Sans',=
'Lucida Grande', Verdana, Arial, sans-serif; font-size: 13px; ">&q=
uot;Hashes have a=A0<em>default value</em>=A0that is returned when accessin=
g keys that do not exist in the hash. By default, that value is=A0<tt>nil</=
tt>." from=A0</span><meta http-equiv=3D"content-type" content=3D"text/=
html; charset=3Dutf-8"><a href=3D"http://rdoc.info/docs/ruby-core/1.9.2/Has=
h">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><meta http-equiv=3D"content-type" content=3D"text=
/html; charset=3Dutf-8"><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">http://rdoc.info/docs/ruby-core/1.9.=
2/Hash</a> =A0is good resource, These pages are some courses for learn ruby=
:</div>
<div><span class=3D"Apple-style-span" style=3D"font-family: Arial, 'Hel=
vetica Neue', sans-serif; font-size: 15px; color: rgb(68, 68, 68); line=
-height: 19px; "><a href=3D"http://tryruby.org/" target=3D"_blank" rel=3D"n=
ofollow" class=3D"twitter-timeline-link" title=3D"http://tryruby.org/" styl=
e=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0=
px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left=
: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width:=
0px; border-left-width: 0px; border-style: initial; border-color: initial;=
color: rgb(0, 132, 180); text-decoration: underline; ">http://tryruby.org/=
</a></span><span class=3D"Apple-style-span" style=3D"font-family: Arial, &#=
39;Helvetica Neue', sans-serif; font-size: 15px; color: rgb(68, 68, 68)=
; line-height: 19px; ">=A0with out install ruby</span></div>
<div><font class=3D"Apple-style-span" color=3D"#444444" face=3D"Arial, '=
;Helvetica Neue', sans-serif" size=3D"4"><span class=3D"Apple-style-spa=
n" style=3D"font-size: 15px; line-height: 19px;"><a href=3D"http://rubykoan=
s.com/" target=3D"_blank" rel=3D"nofollow" class=3D"twitter-timeline-link" =
title=3D"http://rubykoans.com/" style=3D"margin-top: 0px; margin-right: 0px=
; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0p=
x; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-ri=
ght-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-st=
yle: initial; border-color: initial; color: rgb(0, 132, 180); text-decorati=
on: underline; ">http://rubykoans.com/</a>=A0for learn ruby</span></font></=
div>
<div><font class=3D"Apple-style-span" color=3D"#444444" face=3D"Arial, '=
;Helvetica Neue', sans-serif" size=3D"4"><span class=3D"Apple-style-spa=
n" style=3D"font-size: 15px; line-height: 19px;"><br></span></font></div><d=
iv><font class=3D"Apple-style-span" color=3D"#444444" face=3D"Arial, 'H=
elvetica Neue', sans-serif" size=3D"4"><span class=3D"Apple-style-span"=
style=3D"font-size: 15px; line-height: 19px;"><span class=3D"Apple-style-s=
pan" style=3D"color: rgb(0, 0, 0); font-family: Verdana, sans-serif; line-h=
eight: 16px; font-size: 12px; ">Eng'r. Bernardo Galindo</span></span></=
font></div>
<div><font class=3D"Apple-style-span" color=3D"#444444" face=3D"Arial, '=
;Helvetica Neue', sans-serif" size=3D"4"><span class=3D"Apple-style-spa=
n" style=3D"font-size: 15px; line-height: 19px;"><font class=3D"Apple-style=
-span" color=3D"#000000" face=3D"Verdana, sans-serif" size=3D"3"><span clas=
s=3D"Apple-style-span" style=3D"font-size: 12px; line-height: 16px;">Ruby o=
n Rails developer</span></font></span></font></div>
<div><font class=3D"Apple-style-span" color=3D"#444444" face=3D"Arial, '=
;Helvetica Neue', sans-serif" size=3D"4"><span class=3D"Apple-style-spa=
n" style=3D"font-size: 15px; line-height: 19px;"><font class=3D"Apple-style=
-span" color=3D"#000000" face=3D"Verdana, sans-serif" size=3D"3"><span clas=
s=3D"Apple-style-span" style=3D"font-size: 12px; line-height: 16px;">Fronte=
nd developer</span></font></span></font></div>
<div><font class=3D"Apple-style-span" color=3D"#444444" face=3D"Arial, '=
;Helvetica Neue', sans-serif" size=3D"4"><span class=3D"Apple-style-spa=
n" style=3D"font-size: 15px; line-height: 19px;"><font class=3D"Apple-style=
-span" color=3D"#000000" face=3D"Verdana, sans-serif" size=3D"3"><span clas=
s=3D"Apple-style-span" style=3D"font-size: 12px; line-height: 16px;">at Cro=
wdInteractive =3D> <a href=3D"http://www.crowdint.com">www.crowdint.com<=
/a></span></font></span></font></div>
<div><font class=3D"Apple-style-span" color=3D"#444444" face=3D"Arial, '=
;Helvetica Neue', sans-serif" size=3D"4"><span class=3D"Apple-style-spa=
n" style=3D"font-size: 15px; line-height: 19px;"><font class=3D"Apple-style=
-span" color=3D"#000000" face=3D"Verdana, sans-serif" size=3D"3"><span clas=
s=3D"Apple-style-span" style=3D"font-size: 12px; line-height: 16px;">member=
of @rubyc_ =3D> ruby community, ruby colima</span></font></span></font>=
</div>
<div><font class=3D"Apple-style-span" color=3D"#444444" face=3D"Arial, '=
;Helvetica Neue', sans-serif" size=3D"4"><span class=3D"Apple-style-spa=
n" style=3D"font-size: 15px; line-height: 19px;"><font class=3D"Apple-style=
-span" color=3D"#000000" face=3D"Verdana, sans-serif" size=3D"3"><span clas=
s=3D"Apple-style-span" style=3D"font-size: 12px; line-height: 16px;"><br>
</span></font></span></font></div><div><font class=3D"Apple-style-span" col=
or=3D"#444444" face=3D"Arial, 'Helvetica Neue', sans-serif" size=3D=
"4"><span class=3D"Apple-style-span" style=3D"font-size: 15px; line-height:=
19px;"><font class=3D"Apple-style-span" color=3D"#000000" face=3D"Verdana,=
sans-serif" size=3D"3"><span class=3D"Apple-style-span" style=3D"font-size=
: 12px; line-height: 16px;">@bernardo466<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">[email protected]</a>></span><br><blockquote class=
=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd=
ing-left: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 #1 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><br></div>
--0016367f9e02d4dbb40496f43f1d--