Discussion:
Class:Hash
Slevin McGuigan
2010-12-05 21:29:34 UTC
Permalink
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:/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:/home/egon> cat /etc/lsb\-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=9.10
DISTRIB_CODENAME=karmic
DISTRIB_DESCRIPTION="Ubuntu 9.10"
***@egon:/home/egon> ruby -v
ruby 1.9.1p243 (2009-07-16 revision 24175) [i486-linux]
宋卫
2010-12-08 01:58:20 UTC
Permalink
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¡®s values as keys, and the keys as values.

h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 }
h.invert #=> {0=>"a", 100=>"n", 200=>"d", 300=>"y"}
----------------------------------------------------------
maybe you can find your answer.

At 2010-12-06 05:29:34£¬"Slevin McGuigan" <***@googlemail.com> wrote:
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 bothhttp://rdoc.info/docs/ruby-core/1.9.2/Hash#key-instance_method andhttp://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:/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:/home/egon> cat /etc/lsb\-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=9.10
DISTRIB_CODENAME=karmic
DISTRIB_DESCRIPTION="Ubuntu 9.10"
***@egon:/home/egon> ruby -v
ruby 1.9.1p243 (2009-07-16 revision 24175) [i486-linux]
Slevin McGuigan
2010-12-08 14:56:00 UTC
Permalink
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 respective
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 of
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
Post by 宋卫
my ruby version is 1.8.7
may be you can view
http://www.ruby-doc.org/core-1.8.7/index.html
--------------------------------------------------
Returns a new hash created by using hsh‘s values as keys, and the keys as
values.
h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 }
h.invert #=> {0=>"a", 100=>"n", 200=>"d", 300=>"y"}
----------------------------------------------------------
maybe you can find your answer.
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
Linux egon 2.6.31-22-generic #68-Ubuntu SMP Tue Oct 26 16:38:35 UTC 2010 i686 GNU/Linux
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=9.10
DISTRIB_CODENAME=karmic
DISTRIB_DESCRIPTION="Ubuntu 9.10"
ruby 1.9.1p243 (2009-07-16 revision 24175) [i486-linux]
------------------------------
眑易163/126邮箱癟分癟兌容iphone ipad邮件收发<http://help.163.com/special/00753VB9/126mail_adv_guide.html?id=2840>
bernardo galindo
2010-12-09 06:21:33 UTC
Permalink
"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
Post by Slevin McGuigan
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
Linux egon 2.6.31-22-generic #68-Ubuntu SMP Tue Oct 26 16:38:35 UTC 2010
i686 GNU/Linux
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=9.10
DISTRIB_CODENAME=karmic
DISTRIB_DESCRIPTION="Ubuntu 9.10"
ruby 1.9.1p243 (2009-07-16 revision 24175) [i486-linux]
Slevin McGuigan
2010-12-12 13:32:43 UTC
Permalink
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
Post by bernardo galindo
"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
---entry----- return
testhash["1"] => "a" because the key "1" exist and has value "a"
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.
result = testhash.keys return an array of all keys that exist
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,
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
@bernardo466
Hi *,
Post by Slevin McGuigan
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
Linux egon 2.6.31-22-generic #68-Ubuntu SMP Tue Oct 26 16:38:35 UTC 2010
i686 GNU/Linux
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=9.10
DISTRIB_CODENAME=karmic
DISTRIB_DESCRIPTION="Ubuntu 9.10"
ruby 1.9.1p243 (2009-07-16 revision 24175) [i486-linux]
mathew
2010-12-12 15:36:46 UTC
Permalink
Regarding "Hashes enumerate their values in the order that the
corresponding keys were inserted":

Is it really a good idea to specify the order in which keys are
returned? That could have major implications later on if someone wants
to rewrite Hash to increase performance.


mathew
Roger Pack
2010-12-29 05:00:41 UTC
Permalink
Regarding  "Hashes enumerate their values in the order that the
Is it really a good idea to specify the order in which keys are
returned? That could have major implications later on if someone wants
to rewrite Hash to increase performance.
Unfortunately it's something new introduced in 1.9.2, and I think it's
here to stay, especially if documentation mentions it now...
Roger Pack
2010-12-29 05:03:06 UTC
Permalink
Post by Slevin McGuigan
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)
That sounds like a reasonable addition.
The only "blessed" documenation source is the ruby core source code
itself. I'd write up a patch and submit it.

http://www.ruby-lang.org/en/community/ruby-core/
Slevin McGuigan
2010-12-31 12:46:14 UTC
Permalink
Post by Roger Pack
Post by Slevin McGuigan
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)
That sounds like a reasonable addition.
The only "blessed" documenation source is the ruby core source code
itself. I'd write up a patch and submit it.
http://www.ruby-lang.org/en/community/ruby-core/
Just to make sure we have the same understanding (would be pretty goofy
if my first patch to be submitted would be identical to the one you
submitted just a couple of days ago... :o )

By saying "I'd write a patch" you mean "if I were you I would write a
patch. I don't plan to write one".

Thanks to all. I will write a patch. (this will take a while, I first
need to get into the rules & tools etc. ...)

Best Regards and a happy new year!
SMG

Loading...